Ok I have to make a simple GET request that work in Postman but not in the Ionic code. I have determined that the reason is that the API treats incoming requests differently based on Content-Type. application/json works text doesn't.
headers: {
"Content-Type": 'application/json; charset=UTF-8',
"Accept": 'application/json; charset=UTF-8'
},
As you can see I'm setting the header to try to force application/json but when I sniff the request it has changed to text/html somehow. How do I force Ionic to take my Content-Type as definitive?
I managed to get it to work with the following headers
var headers = {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "POST, GET, OPTIONS, PUT",
"Content-Type": "application/json",
"Accept": "application/json"
};
Related
I am using axios#0.21.1 and I want to validate the response headers.
I am unable validate the headers "Content-Type" and "Content-Encoding" from a GET response.
"Content-Type": No matter what content-type i pass in request, the content-type in response is always application/JSON.
Example Code Snippet:
if (<token is present>) {
request.headers = {
authorization : 'Bearer ${token}'
}
} else {
config.auth = {}
}
config.headers = Object.assign(config.header, {
'content-type': application/<custom content>,
'accept-encoding': 'gzip, deflate, br'
}
await axios.get(endPoint, config)
.then(response => {
return response
}*
When i am checking response.header, i see that content-type is showing as "application/json" instead of the custom type. But when i hit the same url in POSTMAN i could see that content-type is as expected.
Content-Encoding: I want to validate the content-encoding in the response, but what i learnt is axios does not return content-encoding header in the response and when i check their github, they are asking to use axios.interceptors. I tried using interceptors but still i am not seeing the header in response. But this header is present in response when i try in POSTMAN. There have been some solution say CORS needs to be enabled in server side. I am strictly asking it from QA point of view because we cannot enable CORS in server side.
Any help is highly appreciable.
Try:
axios.post(your-url, {
headers: {
'Content-Encoding': 'gzip'
}
})
or
axios.post(your-url, {
headers: {
'Accept-Encoding': 'gzip',
}
})
This is by design: https://axios-http.com/docs/req_config
I also ran into this and couldn't find a solution. Ended up using node-fetch instead.
I'm having trouble setting the Content-Type header in axios.
Here's my code:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.body),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
And here's the request headers:
Accept is set correctly but Content-Type is not. (Confirmed by removing Accept in my code, in which case the request header reverts to json isntead of vnd.api+json.)
When I change Content-Type to ContentType, then I see ContentType in the Response headers, so the problem is specifically with Content-Type.
It turns out that this error was the result of having an empty data; the property was called data, but I mistakenly called body:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.data),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
I am trying to send a http request using the http plugin on version ^0.12.0+4 (the latest one).
http.Response response = await http.post(
http://localhost:8085,
body: jsonEncode({"test": "test"}),
headers: {
"accept": "application/json",
"content-type": "application/json"
},
);
I have a SpringBoot backend running which stated, that the request body is empty. Debug attempts with Postman worked fine.
I tried the Dio package as well with the same result.
I was trying to make a call to a REST service using the Dio plugin, but kept getting HTTP 400 response code. I thought I was doing everything right by setting the content type and response type options to JSON:
Response response = await Dio().get(
'https://api.example.com/v1/products/$productId',
queryParameters: {},
options: Options(
contentType: ContentType.json,
responseType: ResponseType.json,
headers: {'Authorization': 'Bearer $MY_API_KEY'}
),
);
However, it turns out that I needed to add a Content-Type header as well:
headers: {'Authorization': 'Bearer $MY_API_KEY'}, 'Content-Type': 'application/json' };
So now I'm confused - what exactly does the contentType option do? I thought it was analogous to setting the Content-Type header manually?
I've tried this locally using dio: ^3.0.10 and it seems that ContentType.json is an invalid value for contentType.
Digging through the documentation for dio, Headers.jsonContentType should be used.
the following code should work, shouldn't it?
import requests
import json
url = "https://api.dropboxapi.com/2/files/delete_v2"
headers = {
"Authorization": "Bearer <access-token>",
"Content-Type": "application/json",
"Dropbox-Api-Select-Admin":
"dbmid:AADnRVGZHenLtFbLVdHDkqEJg3Dou4hWF4g"
}
data = {"path": "id:Kd_cXYig9pAAAAAAAAAARQ"}
print(requests.post(url, headers=headers, data=json.dumps(data)).content)
returns:
{"error_summary": "path_lookup/not_found/", "error": {".tag": "path_lookup", "path_lookup": {".tag": "not_found"}}}
Tried using Dropbox-Api-Select-User, and delete(v1) and permanently_delete and all failed with the same error.
FYI download worked.
(only with Select-Admin, but worked).
Thanks Greg.
Added "Dropbox-Api-Path-Root" header with the parent_shared_folder_id and the api call worked!
headers = {
"Authorization": "Bearer f7I8uYSFpxAAAAAAAAAFSs-VIz17DnoqmEYXp0MeitxIzXBCYCT4v0Bb4N4_cbxK",
"Content-Type": "application/json",
"Dropbox-Api-Path-Root": "{\".tag\": \"namespace_id\", \"namespace_id\": \"2857852064\"}",
"Dropbox-Api-Select-Admin": "dbmid:AADnRVGZHenLtFbLVdHDkqEJg3Dou4hWF4g"
}