I guess I'll move this to the top because none of what's been said so far addresses my question. There are some response headers in Postman that I don't get in Dart. I need to access those values in order for my application to work. How do I get access to the Headers that are in Postman but Dart doesn't seem to have?
I'm using the Dart/Flutter http package in general it does everything I need it to. What I've run into trouble recently is that it doesn't return some non-standard headers as part of a post request. For example, I'm trying to make the following request:
POST https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Patient?_format=json&_pretty=false
Headers: {"Content-Type": "application/fhir+json", "Authorization": "Bearer $BearerToken"}
Body: {"resourceType":"Patient","identifier":[{"type":{"coding":[{"system":"http://hl7.org/fhir/sid/us-ssn","code":"SB"}]
},"system":"urn:oid:2.16.840.1.113883.4.1","value":"444114567"}],"name":[{"use":"usual","text":"Derrick
Lin","family":"Lin","given":["Derrick"]}],"gender":"male","birthDate":"1973-06-03"}
Note, this request succeeds. It returns a Status Code of 201 and I've checked the server and the Patient is successfully created. However, the Response headers are:
{
"cache-control": "no-cache,no-store",
"content-length": 0,
"content-type": "application/fhir+json; charset=utf-8",
"expires": -1,
"pragma": "no-cache"
}
Now, I've tried stopping this request right before posting so I ensure I have all of the correct parameters. And if I copy the same values into Postman, I still recieve a Status of 201, but I receive these as the Response Headers:
{
"Expires": -1,
"Location": "Patient/eoc0yXThvv5aQEdz-kjaSWQ3",
"Allow-Control-Allow-Headers": "origin, authorization, accept, content-type, x-requested-with, Epic-User-ID, Epic-User-IDType, Epic-Client-ID, soapaction, Epic-MyChartUser-ID, Epic-MyChartUser-IDType",
"Allow-Control-Allow-Methods": "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS",
"Allow-Control-Allow-Origin": "*",
"Allow-Control-Allow-Credentials": true,
"cache-control": "no-cache,no-store",
"content-length": 0,
"content-type": "application/fhir+json; charset=utf-8",
"pragma": "no-cache"
}
I need access to these extra headers in dart, specifically the "Location" header. To answer one of the questions below, as far as the code for creating the request, the first is going through the some Oauth2 Code to get the Bearer token. After that, it's calling a POST method on a class that Extends http.Client.
#override
Future<http.Response> post(Uri url,
{Map<String, String>? headers,
Object? body,
Encoding? encoding}) async =>
await http.post(
url,
headers: await newHeaders(headers),
body: body,
encoding: encoding,
);
#override
Future<Map<String, String>> newHeaders(Map<String, String>? headers) async {
headers ??= <String, String>{};
if (client?.credentials.accessToken != null) {
headers['Authorization'] = 'Bearer ${client!.credentials.accessToken}';
}
headers.addAll(authHeaders ?? <String, String>{});
return headers;
}
And again, the request succeeds, it successfully posts the resource, the resource is created, and I get a 201 status code showing it was created. What I don't get is the full set of response headers.
Does anyone have any idea if I'm creating the request incorrectly, if this is something wrong with the http package, or something wrong with the Dart SDK?
There are CORS headers for allowing cross domain access to a web browser:
"Allow-Control-Allow-Headers": "origin, authorization, accept, content-type, x-requested-with, Epic-User-ID, Epic-User-IDType, Epic-Client-ID, soapaction, Epic-MyChartUser-ID, Epic-MyChartUser-IDType",
"Allow-Control-Allow-Methods": "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS",
"Allow-Control-Allow-Origin": "*",
"Allow-Control-Allow-Credentials": true,
This is related to redirecting a web browser to a new location after creating a resource.
"Location": "Patient/eoc0yXThvv5aQEdz-kjaSWQ3",
My Post API need a customer_id in body but also need a bearer token. I am passing it using following code
var myId="1005",
var token="my Token here"
var response = await http.post(Uri.parse("http://haulers.tech/jashn/mobile/home/shoutouttype"),
body: ({
"customer_id":myId.toString,
}),
headers: ({
"Authorisation": token.toString, //here I want to pass Bearer Token
})
);
This code return status code 401.
Pay attention to the word Bearer its must be Capitalized other ways it wont work, not sure whats the reason, but for flutter http calls make sure to capitalize that word like this
var response = await httpClient.post(
url,
headers:{
"Accept": "application/json",
"Content-type": "application/json",
"Authorization": "Bearer $token"
},
body :{some body});
Bearer tokens are usually sent preceded with Bearer : the following key value pair:-
"Authorization": "Bearer {TOKEN}"
Should work.
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 get an access token from paypal's authorization api.
When I make post request to the api I get UNSUPPORTED_MEDIA_TYPE i.e. 415 response.
Below is the snippet that I used.
const auth = await fetch(PAYPAL_OAUTH_API, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${ basicAuth }`
},
body: JSON.stringify({"grant_type": "client_credentials"})
});
I have fixed my issue by setting Content-Type to application/x-www-form-urlencoded.
My guess is paypal accepts only application/x-www-form-urlencoded for authorization api.
I ran into same issue, and the solution is following (using Postman):
Select POST
Add Token into Authorization, type is Bearer-Token
Select Content-Type: application/json in headers
Use RAW as body, and in TEXT dropdown, select JSON (application/JSON)
Copy body as raw object and change info accordingly.
Step 4 and 5 are what solved the error, you must send raw json object.