I am trying to make a http post request with HTTPoison.
I want to pass json data with header, which contains "Authorization": Bearer #{token}.
In order to achieve that, I have tried,
headers = [{"Authorization": "Bearer #{token}"}, {"Content-Type", "application/json"}]
body =
%{
id: id,
name: name,
...
}
HTTPoison.post(url, body, headers)
But it triggers a syntax error that syntax error before: "Authorization". And I have been searching for right syntax for headers but still no luck..
What is correct syntax for headers ?
Thanks in advance..
I believe, the correct syntax should be as follows:
headers = ["Authorization": "Bearer #{token}", "Content-Type": "application/json"]
or, if you prefer the "tuple" way of defining keyword, this would be the equivalent:
headers = [{:"Authorization", "Bearer token"}, {:"Content-Type", "application/json"}]
Hope it helps!
Related
really that simple, making a request with github api: https://docs.github.com/en/rest/reference/checks#list-check-runs-for-a-git-reference
I am trying to find the check runs for a particular branch I have. Below is the url I GET from:
url = ...api/v3/repos/{repo_fullname}/commits/{branch}/check-runs'
Here are my headers:
headers = {
'Authorization': 'token ' + token,
"Accept": "application/vnd.github.v3+json"
}
I get hit with the: 415 Client Error: Unsupported Media Type for url...
Please help, been banging my head for hours. Thanks!
Perhaps you could try a different accept? I work with octokit for node and this fixed my problem, maybe it would help in this case.
headers = {
'Authorization': 'token ' + token,
"Accept": "application/vnd.github.antiope-preview+json"
}
I have this code where there are multiple Content-type keys is used in this POST request.When this request is run I always get an error that a duplicate key is used.I want to know how I can use multiple Content-type in one Content-type key.The header of the request looks like below:-
headers: {
'Content-Type':'application/json,
'Access-Control-Allow-Origin': '*',
'Content-Type': 'multipart/form-data; charset=utf-8; boundary=' + Math.random().toString().substr(2)
}
You can’t. Are you sending JSON or Form-Data? It can’t be both so has to be one or the other.
I set a header for post a request with application/x-www-form-urlencoded form body;
here's my code
var headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
fetch(url, {
method: "POST",
mode:"cors",
header:headers,
body: qs.stringify(params)
})
But Content-type changed autoly in the request
Content-Type text/plain;charset=UTF-8
So that I can't receive this request params correctly.
Any one meet that?
CORS only allows a subset of content types. I lost lots of hours to this one. :)
See this answer. Even though application/x-www-form-urlencoded could be possible in a CORS request, I think it's very likely that your header is being overwritten by setting mode: 'cors' in your fetch call.
I have tried to send axios get request using vue.js and it worked just fine when there was no need to send headers. However, when it was required to send an authorization jwt, i was getting CORS error: "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource." I don't know why is this problem occurring since there is Access-Control-Allow-Origin: '*' header in the response. My code is the following:
axios.get(url, {
headers: {
'Authorization': 'Bearer TOKEN'
}
})
.then(function (response) {
console.log(response.data)
})
The weirdest thing is when I use querystring.stringify or JSON.stringify on the header, I don't get the error 403(forbidden), but just an error 401 - Unauthorized. I tried with variable and with the token itself and it didn't work.
I tried to send a post request in order to get a web token with required data - username an password and it worked. I was able to get the token.
I made a whole bunch of research the last two days on this and I found different kind of request structure and configs which I tried all of them, but none were efficient. Is there a way to check if the request is being send with the header? Is something else the problem? If someone can help, I would appreciate. Thanks.
I think you should add this code to the bootstrap.js (or where the axios is defined):
window.axios = require('axios'); // I think its already added
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest'
};
You didn't mention, but I guess you use laravel, or other framework, what is protected from csrf attack, thats why you need to add the generated token to your ajax request header.
I want to remove access-encoding header from $http.get request. Tried via following, but not any luck.
delete $httpProvider.defaults.headers.common['Accept-Encoding'];
$http({ method: 'GET', url: apiUrl, data: '', headers: { 'Content-Type': 'application/json', 'accept-encoding': null } });
Please share your thoughts.
For the very same purpose, I am following this fiddle,
http://fiddle.jshell.net/X2p7r/344/
also as per documentation of AngularJS
To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g.
$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }
I am able to modify 'Accept' and 'Content-Type' headers but not able to do same with Accept-Encoding, doesn't change no matter what I try it remains same.
Thanks in Advance for any help.
Edit:https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
I tried to set Accept-Encoding according to the ways mention in above link.