I need to send string data to an external url which is an API for clients, but I always get the server sending back an error, and then I checked the log in the server which is written in JAVA, it throws an exception
"java.lang.NumberFormatException: For input string: "{"success":false,"errorCode":"500","message":"Unknown Error!","value":null,"totalPageCount":0,"footerValue":null}"
and here's my piece of php code:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $params
),
));
$received = file_get_contents(URL, false, $context);
echo $received.'<br>';
I'm pretty sure the data I send to the server is in correct format as I have the access to the log, and I know that all response parameters from the server are in JSON, UTF-8 and post method, is there something I missed?
If more information needed please let me know, thanks.
You need to parse the string. You are getting NumberFormatException and this is because your server is receiving data which is not a numeric.
Integer.parseInt() // in java
You can parse the content in server side this way or send numeric data.
Another method:
Integer.valueOf() // in java
Related
I am working on a web application where on clicking a button on the UI a GET request is made to the server as below:
https://mywebapp.com?info=%5B%7B%22first%22%3A%22abcd%22%2C%22second%22%3A%22efgh%20ijkl%22%2C%22third%22%3A%22mnop%22%7D%5D
Basically the value I am passing as info is:
[{"first":"abcd","second":"efgh ijkl","third":"mnop"}]
However, when I read this passed value on server, I found it to be received as:
[{first:abcd,second:efgh ijkl,third:mnop}] i.e. all the double quotes are removed.
Now when I try to parse it into json, it fails.
Could you please suggest how could I fix the issue so that the json is received as expected.
Please note that it is an existing big application and I can't change any server level settings.
Thanks
To keep the double qoutes as is you have to send the json in single qoutes
i,e convert the json into string then send it, because the GET/POST request don't recognize json format
I am doing Python client HTTPSConnection.request('POST', url, data, headers) in python.
The data is b'1502944466046' which is request body
Debug print shows:
send: b'POST /path/to/someresource HTTP/1.1\r\nHost: 172.1.2.3\r\nAccept-Encoding: identity\r\nContent-Length: 13\r\nContent-Type: application/xml;charset=UTF-8\r\nAccept: application/xml\r\nAuthorization: Basic encodeduserPwd==\r\n\r\n'
send: b'1502944466046'
On Java server, I get org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not 1 (position: START_DOCUMENT seen 1... #1:1)
The data for the POST is just the 'data' variable below derived as follows:
name = str(int(round(time.time() * 1000))) # time in millisec used
data = name.encode('utf-8')
The Java REST controller has a method:
#RequestMapping(value=sameurlaspythonurlabove, method=RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
public void createMyObject(#RequestBody String name)
The method is not hit because of the exception above.
The body is not XML, but server is trying to parse XML, I think, from error above.
If I set Content-type = 'text/plain', then server gives org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
Any ideas how to circumvent?
The problem is that your code is expecting XML but you are giving it JSON that starts with 1.you can convert your request body in xml format then you can try.
I need to send the json object in get request. I installed chrome Postman extension but i am not getting how can i send json object in GET request ?
Postman provides the way to send json data in Post request by adding the header as application/json and then add the json data under raw form.
How to send the json data in GET request ? Do i need to append it in URL ?
It's bad solution to send any objects using get request. But you can send it as a url parameter using url encoding:
String url = "http://example.com/query?json=" + URLEncoder.encode(json, "UTF-8");
In POSTMAN you can send body data in GET request. If you try to append in the URL using url encoding you will get error.
Try to convert json object into string and send it in the URL parameters and see if it works.
Also if your backend server allows only to send data as URL parameters and your URL is long (i.e approx 2048 characters) then I am not sure whether this will work.
If above solution doesn't work then I think you can achieve this using curl. CURL is a tool for doing all sorts of URL manipulations and transfers. You can generate cURL code using Postman. Here is the reference
You can use google chrome Postman Extension
it allows you to send and see any type of data.
I am developing an application with AngularJS (client side) and Java (server side) using RESTful services (with Jersey).
In the client I have something like this
$http({
url : "/Something/rest/...",
method : "POST",
headers : {
...
'text' : text
}
});
on the server side my Java method header looks like this
public JSONObject myMethod(#HeaderParam("text") String text [...]) throws JSONException
And I'm getting this error when trying to send a request
Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Some text here formatted with \n and \t' is not a valid HTTP header field value.
Is this because of the formatting? Is it because the text is quite long?
What should I change? Please guide me
I had faced this problem. Please make sure that in your code header name doesn't contain space. i.e. 'designId ' is invalid, as it contain space at end
Are you sending your 'text' in headers on purpose? This is in general not a place for doing this.
For sending content through POST(or GET) you should use 'data' field.
So:
$http({
url : "/Something/rest/...",
method : "POST",
data: {
...
'text' : text
}
});
,and on the server side similar change for getting your data from POST.
You don't need and shouldn't mess with headers unless you know what you are doing.
I experience this a few days back. I simply clear my cookies and other site data and it was able to resolve the issue.
While executing this below Dojo code the call back mehod is calling the onFailure.
But if i will do
console.warn("Resp Code:"+ioargs.xhr.status);
It gives me 200 as status code why this is happening,it should go to the load but it is calling the error.
dojo.xhrGet({ preventCache : "true",
url : path,
sync:true,
load : onSuccess,
error : onFailure,
handleAs : "json"
});
More than likely, since you've told the request to handle the response as json, the response you are getting back is not actually json, which can generate the error. From the Live Docs # dojotoolkit.org:
This parameter specifies how to handle the data returned from the server. It usually takes values of 'text', 'xml', or 'json'. What those values do is instruct it to try and hand the data to the asynchronous callback functions in that format. Naturally if you specify a format that doesn't match what the server sends you will likely get an error.
Make sure the response is sending back valid JSON and the server is sending it as the application/json content-type, otherwise set your handleAs to text.