Is it IMPOSSIBLE to POST JSON with XDomainRequest? - ajax

As I am not able to find a way to set the content-type to JSON for the data I sent via XDomainRequest,
I naturally ask:
Is it IMPOSSIBLE to POST JSON with XDomainRequest?
"Only text/plain is supported for the request's Content-Type header"

Looking at some other questions, I'd say this is possible.
For example: How to send JSON data in XDR using the POST method

You can still POST the JSON as plain text and have the server side read the POST body directly and handle it, even Content-Type is missing for the sick of IE.

According to http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
You cannot send content-type : application/json in xdr. You can not even send any other custom header.
xdr is only limited to text/plain
The best you can do is use JSON.stringify() and parse it on server side.
xhr.send(JSON.stringify(jsonObject))

Related

Handling text/plain request in Laravel (sent via navigator.sendBeacon)

Problem:
I am trying to get the content / body from a text/plain POST request in a Controller in Laravel 8.
Nothing in the docs or numerous google searches worked.
I have tried:
$request->input();
$request->all();
$request->json();
json_encode($request);
All of these seem to show that the request is completely empty.
Context
This may not be relevant to the solution, but might help other's who are trying to google this problem: The request I am trying to handle with my controller is being sent by navigator.sendBeacon on the client side. The request body is actually stringified JSON, but sendBeacon does not allow you to send requests with content-type JSON. Devtools show the content-type header for this request as 'text/plain'.
My Answer:
Use $request->getContent() to get the content of a text/plain HTTP request.
And then, if you are in a situation like mine where the text is actually JSON, use json_decode($request->getContent(),true) to get a standard PHP array from the content which you can use in your Controller.
It turned out to be quite simple, but the information was not in the docs, or in any online searches so I thought it would be worthwhile to post to SO anyways...
You can send the data as JSON like this instead
data = new Blob([JSON.stringify(data)], {type : 'application/json'})
navigator.sendBeacon('/your/route/here', data)

POST request with Content-Type application/x-www-form-urlencoded and

My requirement is very simple.
Call POST request with id and password.
Header has Content-Type = application/x-www-form-urlencoded
and data is also passed as urlencoded like below
Response is coming in xml format.
I tried a lot of examples from everywhere but nothing seems to be working. It gives me back 401 Unauthorized which is an error that target API throws if request is not in proper format.
http://zetcode.com/java/getpostrequest/
Exactly what I needed.
Java HTTP POST request with HttpURLConnection section on the page did the work.

Mandrill API error for send request

I have a problem while sending a message via Jersey client on Mandrill API. I use Jersey client as follows:
ClientBuilder.newClient()
.register(JacksonJsonProvider.class)
.target(“https://mandrillapp.com/api/1.0/messages/send.json”)
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.json(methodEntity));
Below you can see logged headers, method and content of the API request.
POST https://mandrillapp.com/api/1.0/messages/send.json
Accept: application/json
Content-Type: application/json
{"message":{"subject":"Hello World!","text":"Really, Im just saying hi from Mandrill!","to":[{"email":"marcel#xxxx.com","name":"Marcel cccc","type":"to"}],"headers":{},"tags":["test"],"from_email":"info#xxxxx.com","auto_text":true,"preserve_recipients":false},"async":false,"key":"EWIBVEIOVBVOIEBWIOVEB"}
In response to this request I keep receiving following message:
[{"email":"marcel#XXXX.com","status":"rejected","_id":"0ea5e40fc2f3413ba85b765acdc5f17a","reject_reason":"invalid-sender"}]
I do not know what the issue may be, from some posts I figured out I must use UTF-8 to encode my message and headers. But setting encoding to UTF-8 did not do much good. Otherwise the payload seems fine to me and moreover I found on forums that invalid sender can mean any other kind of issue (not just invalid sender which is sad).
I had exactly same problem with
"reject_reason":"invalid-sender"
You probably check already similar question Mandrill “reject_reason”: “invalid-sender”
Try it if it helps. I realize that you also missing header parameter in your request
e.g. User-Agent: Mandrill-myclient/1.0
Please try also add this parameter to your Jersey Client setup as following:
ClientBuilder.newClient()
.register(JacksonJsonProvider.class)
.target(“https://mandrillapp.com/api/1.0/messages/send.json”)
.request(MediaType.APPLICATION_JSON_TYPE)
.header("User-Agent", "Mandrill-myclient/1.0")
.post(Entity.json(methodEntity));
Does it help?

Sending an http POST with netcat

I am trying to send data within the body of my http post. The following code makes the POST but I can't figure out how to get data into the body or any other field besides the http version and URL fields. I've looked and tried different things from other sources on the net to no avail. Ultimately this will be within a bash script - I can\t use CURL or WGET.
printf "POST /postresearch HTTP/5.0\r\n\r\nTHIS IS THE BODY" |nc -n -i 1 10.0.1.11 3000
You need to make a valid HTTP request, which includes using the correct protocol version number and for POST requests, providing an entity length, and possibly a content type.
POST /postresearch HTTP/1.0
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
field=value&field2=value2
I issue a HTTP 1.0 request, specify that the body will be 25 bytes, and specify that I'm sending URL-encoded form parameters (this is how browsers send forms when you submit them).
BTW, don't do this. There is absolutely no situation where you should be implementing your own HTTP client. Use a proper HTTP tool or library.

RESTserver POST request format

I am developing an API using CodeIgniter and the RestServer for CI (see below). I am also using the Firefox RestClient plugin to test the API.
What I am wondering is how to do the test post (what format).
Tried {"desc":"value"} but it did not work. The API is not "seeing" the incoming post fields.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
the post body doesn't need to have a specific format, but the most convenient is to encode the body in the same way web browsers encode form data, specifically Content-Type: application/x-www-form-urlencoded. In particular, the Host and Content-Length headers are not optional, and the Content-Type header is usually needed to tell the server how to interpret the body. A well formed POST request will look like:
POST /path/to/resource HTTP/1.0
Host: example.com:80
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
key=value&key2=value2
It's still up to the server to recognize the content-type header and parse the body that way.
Note that the data is after all the headers, not as part of the request path (in the first line).
Optionally, you can use Proxy Library which i wrote for CI. With that, you can simulate any of possible call to your API(its works for popular REST API too), with more simple syntax instead using cURL...
// An example call to your API end point using POST, will be simply
$this->load->library('proxy');
$this->proxy->http('POST', 'http://somesite.com/api/users', array('username' => 'foo', 'password' => 'bar'));
You can define whatsoever HTTP header too (like API Key or whatever else).

Resources