Calling XMLHTTPRequest from outside javascript - ajax

I need to parse the content resulting from a XMLHttpRequest, not from the browser but using PHP/or PERL or whatever, BUT javascript.So I tried to make a POST based on my understanding of the specific request I need to parse but I get in return a 404 (page not found).
Where is my mistake?
1) Incorrect post message
2) The server understands that my request is not coming from XMLHttprequest and then rejects with a 404.
3) Other I do not know.
Sorry, I cannot post the URL because it is inside the Intranet.
I will post the code written in perl:
use CGI;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $response
= $ua->post( $myUrl,
{ bId => '5',
'c0id' => '0',
'c0mN' => 'cM',
'c0p0' => 'string3.57',
'c0p1' => 'boolean:false',
'c0sN' => 'mD',
'cC' => '1',
'httpSessionId' => '0C23946319623A',
'page' => $relPage,
'scriptSessionId' => '67233543259'
})

You don't have to do anything special with those other methods; just set the request type in the open() call, and send() an appropriate body (the item you're putting in the case of "PUT"; a null argument in the case of "GET").
Do you have a timeout mechanism and event scheduling in place?

Related

HTTParty post method doesn't return status code when adding follow_redirects = false along with other headers and cookies?

The HTTParty methods are like HTTParty.XXX(urlGoesHere, and Args Here)
I'm doing the following:
params = {:UserName => "uname", :Password => "pwd"}
cookie_hash = HTTParty::CookieHash.new
cookie_hash.add_cookies("key1=val1")
cookie_hash.add_cookies("key2=val2")
options = {
:body=>params,
:headers => { 'Cookie' => cookie_hash.to_cookie_string,
'Accept' => something },
:follow_redirects => false
}
URLUserNamePwd = HTTParty.post(myURL, options) # Is this the right way to do?
When I check the http status code, body I get nothing. When I check in the browser development console, I see 302 redirect, and in response headers I see lot of header pairs returned.
What is the URL you're attempting to hit? 302 Found will be paired with a Location header containing the URL where the resource can be found.

Laravel HTTP Client Bad Request on Discord API Access Token Exchange

I have a laravel application setup and I'm configuring a Discord auth system.
I have sent & requested authorization & recieved a confirmation code back.
When I try to exchange the code for an access token I'm receiving a 400 bad request and I'm not sure why.
I'm new to laravel and can't seem to find any sort of error that may help to pin point the problem.
Controller function that discord redirects after authorization
public function exchange(Request $request) {
//exchange discord code for access_token
$code = Request::get('code',false);
$params = array(
'grant_type' => 'authorization_code',
'client_id' => env('CLIENT_ID'),
'client_secret' => env('OAUTH2_CLIENT_SECRET'),
'redirect_uri' => Request::root().'/discord/return',
'scope' => 'identify guilds guilds.join',
'code' => $code
);
$access_token = Http::withOptions([
'debug' => true,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
],
'json' => $params
])->post('https://discordapp.com/api/oauth2/token');
dd($access_token);
return redirect()->route('return.discord',['token' => $access_token->access_token]);
}
I've dumped out $params and all the data is pulling in as it should.
Guzzle dumps this:
[CONNECT] [FAILURE] severity: "2" message: "HTTP/1.1 400 Bad Request " message_code: "400" [MIME_TYPE_IS] message: "application/json" [FILE_SIZE_IS] message: "Content-Length: 26" bytes_max: "26" [PROGRESS] bytes_max: "26"
I know that 400 bad request means an issue with the data being sent along, but I can't figure out what I'm doing that's not producing the correct result. The documentation states the required content type which I have set, so I'm really scratching my head here.
Any help would be much appreciated.
Here's the Discord API documentation:
https://discord.com/developers/docs/topics/oauth2#authorization-code-grant
To anyone looking for an answer...
The "invalid grant" error is due to an issue with the Discord API and non-standard requests. I could not get the Laravel HTTP class request to work for exchanging the access token. Instead, I used native php cURL within Laravel.
As for the issue with receiving '400 bad request', that was due to using incorrect function withOptions() as oppossed to asJson()->withHeaders(). Thanks #Zachary Craig!
However, I could not use this in the end due to the problem described above.
If any others find a better solution, let me know :)!
For all who have the same problem.
The solution is to use ->asForm() before the post. This changes the handling of the data from json to form_params. This way the Discord API accepts the parameters.
return Http::asForm()->post($this->endpoint.'/oauth2/token', $data);

API call is working fine when called through Postman, but when called by the web page it bogs and returns failed

I have a laravel application that I'm creating. The web routes calls the API for information. When I test my API it returns data, but when the front end dev calls the API it slows the system and returns a failure. This is a simple GET that was working before but is not working now.
This is the the API call from the web:
$url = 'http://localHost:8000/api/v1.0/theater_movies';
$opts = array('http'=>array('http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHPExample',
'method' => 'GET',
'header' => "Content-type: application/json\r\n")
));
//Basically adding headers to the request
$context = stream_context_create($opts);
$getMovies = json_decode(file_get_contents($url,false,$context), true);
I'm getting the file_get_contents error:
file_get_contents(http://localhost:8000/api/v1.0/theater_movies): failed to open stream: HTTP request failed!
And this is the correct output from Postman

Laravel api header request

Route::resource('get-users','api\UserController#get_users');
Route::resource('register','api\UserController#register');
My Api is giving me the response I checked it using POSTMAN but android developer is asking me that what should he sent in header for my API .
I am not getting that how to create a header I am using laravel and I have written functions in controller .
You should go through this part of the documentation: https://laravel.com/docs/5.6/responses#attaching-headers-to-responses
He maybe wants additional header in the response.
Your return value in api\UserController can be extended like this:
return response($content)
->withHeaders([
'Content-Type' => $type,
'X-Header-One' => 'Header Value',
'X-Header-Two' => 'Header Value',
]);

Get the incoming message on my clickatell number through http api

I want when someone message me on my clickatell number then I want to send this message on my email. I'm using the http request.I have check the http api pdf in which only showing the authentication and send message url . I want the url by whcih I get the message in my script.
I'm using the curl function for request the page.
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://api.clickatell.com/http/auth?user=demo&
password=demo&api_id=123432',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);
It is working and return me the session id and ok status. similarly can I get the incoming message ?
Clickatell has a two-way service which will allow you to receive messages from their service and their implementation documentation can be read here.
In order to receive the notice via email, I would recommend opting for the HTTP callback from Clickatell's servers. This will allow you to write a small script to convert the contents into an email.
Hope that this helps you.

Resources