I was trying to hit a rest API URL which using POST request. It receives "ACCEPT" as "application/json" and "Content-Type" as "application/xml". The response from sever says "Content-Type → application/octet-stream;charset=UTF-8". But what now am getting is some kind of encrypted text as below
N4+8ByEFqdMY+fgXAeMQIe93TUKGNXerqeWOGDWm3IoDK5j7YysPB93ebN3IOBCOXlSOlkOV7Vr6 Cz0+nQnWVp09OmB1Xbazz33uWZrJqzzZ8Z7Zdnj0ZfIWMPKDKrymhR8SbXODr+DPj2b3Mv6kI8uo Q3Bx+AtQ8pCVqZQ5sutIOGvJdLhHNSLDNoWZOs87i3eDjGUobNUEiJFaavfKF7l4XJTtVng2TAqN VU9SF5PAlq16syqxTQ2qdk/NU+BOBv8mIYpoCn5gRhuNgpqyPI3b/EhRHLTCo23gRSTi6vwPHeQf vLLTEqtXJnE80xRNyRc9/XvnxBXzaCuW5JJ19GtBzzeJjRwf79WxFbdR50KTgt98T4fpzT42BWJa 6E7w/D7kH+kPO4GkhR6pB7JsC/PBxahvFsEV2tR1dQpCtWQUI9nmMt85bazfRwY4zOWWS5I/rZdN snKn2Zq+Xc2nzIG+imbAoSjIlp8+m/FSzB5fezLHF91fwTM/D1oDUaia11uLtqN8s1pHaH4K8Vkm Ll6pT5KEe+wqZD92gYiEButwXJpxKW4lEhTRdUtffU/6
How can I read it into plain text for some form of readable text like JSON or XML?
try using below line:-
response.setCharacterEncoding("utf-8");
Related
I have just started swagger for documented api. I am using Swagger inspector for execute the api, before that I was using Postman. I am passing the data in Swagger body but not getting the data, same thing works in Postman but not working here.
Moreover get request works with Swagger but post is not working properly.
Getting response in Postman:
Same thing not working in Swagger:
I have already set the header, but not getting my parameter in post.
That's because you are not setting the Request Headers Content-Type in swagger, as you need to send the POST request you need to send the data via the Body tab and to send the data you need to specify the content type for the request body so do the following
Add a Content-Type : application/json header from the Authentication&Headers tab like below
if you are using any authentication headers for the request when using postman add them too
Now go to the Body tab
and add the following
{
"userName": "123",
}
That's all you need to do now click send and it will show you the data sent, just select any format for the request Headers and then provide the data in that format. Below are the supported formats
I am trying to fetch an external JSON response with my PHP back-end
But my problem is that the external endpoint is returned as the Content-Type: text/plain;charset=utf-8, and that only gives me gibberish when I read it.
string '����������ݒ�J�� ... etc...
Is there a way to encode that response?
This is what I have done:
$response = file_get_contents('external_url');
I have also tried this:
$json = json_decode(file_get_contents('external_url'), true);
It doesn't really matter to PHP what Content-Type the response declares, it doesn't do anything with that information. You're getting exactly the same response body whether the header says text/plain or JSON.
More likely the response is compressed and you need to uncompress it with gzinflate or such.
I'm a building a RESTful API using Zend Framework 2 and Apigility by Zend Framework.
For testing, I use the chrome extension Postman REST-Client.
I can do GET requests and POST requests without problems by sending form-data without problems.
But when I try to do a PUT, PATCH or DELETE request, I get the following error:
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
Accept whitelist in Rest-Service-Config of Apigility:
application/vnd.timber-ms.v1+json, application/hal+json, application/json
Content-Type whitelist:
application/vnd.timber-ms.v1+json, application/json
The content-type of the response is application/problem+json
What can I do to fix this and do successfull PUT/PATCH requests?
Is this a problem with Postman or Apigility?
You're getting the 415 error of Unsupported Media Type when Apigility cannot deserialize the data coming from the client. This recently was called out in the documentation.
I suspect your problem is due to the content-type being sent from postman. Pay special attention to the Content-Type Whitelist listed for the service and make sure it contains the content-type you are sending.
For example, if your service has only has application/json in the Content-Type Whitelist and you send the PUT/PATCH with postman as x-www-form-urlencoded, you will get a 415 error of Unsupported Media Type. If you change postman to send the PUT/PATCH with a content-type of application/json and the request body contains valid JSON, Apigility should accept the request.
You can check the content-type postman is sending by clicking on the "Preview" button just to the right of the "Send" button.
I was having a similar issue in Postman with the unsupported media type responses. However, I experienced this response on both PUT and POST requests using my company's API.
I verified that Postman was the problem here and not the request/API in use by running the same request with another similar Chrome extension called Advanced REST Client. I'm not familiar with Apigility, but pretty sure that Postman is the culprit here.
Hoping there is a fix for this issue as Postman and its collections feature is much easier to use than that of Advanced REST Client.
on postman go to normal tab (or other auths tab) and add header:
Content-Type application/json (or any type you need)
make sure that on the raw tab include the json data
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
I had the same problem,
My solution was to write my data to [Body] in the [raw] in json format. Like this:
{"message": "UPDATED First Post!"}
And in [Headers] :
[KEY] Content-Type
[Value] application/json
I'm setting an API for my server for another developer. I'm currently using Flash AIR to send POST data to my server, and simply extract the variables as in
$command = $_POST['command'].
However, he's not using Flash, and is sending data like so:
https://www.mysite.com POST /api/account.php?command=login HTTP/1.1
Content-Type: application/json
Connection: close
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
My server is returning him an error saying that the 'command' parameter is missing.
What do I need to do my end to extract the $_POST var 'command' from his above data?
I've tried file_get_contents('php://input') and http_get_request_body(), but although they don't error, they don't show anything.
Thanks for your help.
The request claims that it is sending JSON.
Content-Type: application/json
However, this:
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
… is not JSON.
If you get rid of everything before the { then it would be JSON and you should be able to read it with file_get_contents('php://input') (and could then pass it through a decoder.
I've tried file_get_contents('php://input') and http_get_request_body() … they don't show anything.
They should work.
When I print out file_get_contents('php://input') for the comms … I get command=login, yet...
I thought you said you didn't get anything
if(!isset($_POST['command']))
$_POST will only be populated for the two standard HTML form encoding methods. If you are using JSON then it won't be automatically parsed, you have to do it yourself (with valid JSON input (so the additional data would need to be encoded in the JSON text with the rest of the data)), file_get_contents('php://input') and decode_json).
"Content-Type should be www-form-urlencoded" from #Cole (correct answer)
More info here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
The command parameter needs to be part of the data, and the whole thing should be valid JSON. As is, command=login, it is not valid JSON.
Add it to the params object or make a containing object, like
{
command:'login',
params :{"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
}
Say you submitted the form and you have in the Request Body:
data[first]=johnny&data[last]=appleseed
The normal POST request works fine from the web, but when submitted from Fiddler the PHP complains that 'data' is not an array.
I'm not sure how to format the request body such that 'data' gets recognized as an array.
Yes I'm using Content-Type: application/x-www-form-urlencoded.
Thanks
Found the problem, wasn't encoding the request body properly (i.e. wasn't using %5B for '[' and %5D for ']')