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

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.

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)

How to generate HTTP Request response data in JSON format?

How to generate HTTP Request response data in JSON format? I am getting the response data in the HTML format.
In the HTTP Header Manager, I am sending the Content-Type = application/json. In the HTTP request body data I am sending the data in the JSON format.
What are the changes I should make to get the response in the JSON format?
I want to extract some values from the JSON response and use in the script.
Double check your HTTP endpoint specification and requirements. The reasons of getting response can be different, we cannot help without seeing the endpoint details, request and response data.
Among the possible reasons are:
Incorrect HTTP Method used. In this case you can get HTTP 406 response
Invalid URL. In this case you can get HTTP 404 response
Invalid request headers. I.e. missing Content-Type or Accept headers. You can add arbitrary headers using HTTP Header Manager
You're not authorized (missing cookie or token). In this case you can get HTTP 401 or 403 statuses

when sending json request to server I am getting forbidden 403

I am learning jmeter.
I am passing json request in Body of http request. I set content-type as application/json. passing cookie value using cookie manager. I am getting response code as 403. how to resolve it??
I have passed all the headers which is caputred in Firebug.. Only the difference is the captured cookie in the firebug has lot of values but when i run the test, the below only is passing (if I use, Cookie Manager) other than that all are same.. No clue why it is returning 403..
Cookie Data:
connect.sid=s%3AFBcljlVYI2p1WyjuxcDgWQKJ.kSrLYIsSy9T%2FEgSB25yUq0T3qTjpqF69GJhyW9GzJpU; TOKEN=ZU3cf9fKGCPJXM6qi7JX6DVv2%2B3Jw8q%2Flnb2A%3D
403 is a "Forbidden" error. Most likely is that something needs to be sent in the body of your http request, like a token, which needs to be extracted from the log in request.
Add Http authorization manager, specify the url along with the crednetials in it. Keep the encoding mechanism to BASIC_DIGEST. Enable log viewer so that you can trace the exact issue

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?

Is it IMPOSSIBLE to POST JSON with XDomainRequest?

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))

Resources