For some reason, a POST requests fails with a timeout, while an according CURL request works perfectly fine. What could get wrong?
Working CURL request:
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"jwt": "jwt"}' \
https://iam.api.cloud.yandex.net/iam/v1/tokens
Not working Vert.x WebClient request:
#Test
fun `get api token`(vertx: Vertx, testContext: VertxTestContext) {
val webClient = WebClient.create(vertx)
webClient.post(443, "iam.api.cloud.yandex.net", "iam/v1/tokens")
.putHeader("Content-Type", "application/json")
.ssl(true)
.sendJsonObject(JsonObject().put("jwt", "test"), testContext.succeeding {
testContext.verify {
Assertions.assertEquals(200, it.statusCode())
testContext.completeNow()
}
})
}
Related
I need to send a request like the one below.
curl --request POST --url https://dev-jlsubxnitkpok2tw.au.auth0.com/oauth/token
--header 'content-type: application/json' \
--data '{"client_id":"","client_secret":"","audience":"","grant_type":"client_credentials"}'
I am using Ballerina like below
http:Client securedEP = check new ("http://postman-echo.com", {
auth: {
tokenUrl: "xxx/oauth/token",
clientId: "xxx",
clientSecret: "xxx",
scopes: ["read", "submit"]
}
}
I get an error like the one below from the service.
cause: Failed to get a success response from the endpoint. Response code: '403'.
Response body: '{"error":"access_denied","error_description":"No audience
parameter was provided, and no default audience has been configured"}'
How can I achieve this in Ballerina?
You can use the optionalParams parameter in ClientCredentialsGrantConfig to specify that.
http:Client securedEP = check new ("http://postman-echo.com", {
auth: {
tokenUrl: "xxx/oauth/token",
clientId: "xxx",
clientSecret: "xxx",
scopes: ["read", "submit"],
optionalParams: {
"audience": "aud"
}
}
});
Note that the auth record field here is coming from CommonClientConfiguration type.
Stupid question here, but I am pulling my hair out (i'm bald) as to what I could possibly be doing wrong. I am doing a UrlFetchApp.fetch in google app scripts:
var result = UrlFetchApp.fetch(url, { headers: headers });
When using this as my headers, it works:
var headers = {
Authorization: 'Bearer ' + getAccessToken()
}
Now I need to pass in 'method' as 'PUT' instead of the default of 'GET' (above), and and trying like this:
var headers = {
method : 'put',
headers : {
Authorization: 'Bearer ' + getAccessToken()
}
}
When I try sending this, it is not finding the token:
Exception: Request failed for https://api.spotify.com returned code 401. Truncated server response: {
"error": {
"status": 401,
"message": "No token provided"
}
How can I format the headers so that I can pass in the Authorization with token and also pass in 'method' as 'PUT'?
I see in the URLFetchApp.Fetch documentation for Google, that this is passed into options:
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
headers Object a JavaScript key/value map of HTTP headers for the request
method String the HTTP method for the request: get, delete, patch, post, or put. The default is get.
Thank you so much for any help!
phi
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script. Ref And, from I know the call works with a simple GET method, your access token is valid.
curl --request PUT \
--url https://api.spotify.com/v1/me/player/play \
--header 'Authorization: ' \
--header 'Content-Type: application/json' \
--data '{
"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
"offset": {
"position": 5
},
"position_ms": 0
}'
In this case, how about the following sample script?
Sample script:
Please modify data for your actual situation.
var data = {
"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
"offset": {
"position": 5
},
"position_ms": 0
};
var options = {
method: 'put',
headers: { "Authorization": 'Bearer ' + getAccessToken() },
contentType: "application/json",
payload: JSON.stringify(data)
};
var url = "https://api.spotify.com/v1/me/player/play";
var res = UrlFetchApp.fetch(url, options);
console.log(res.getContentText());
Note:
I think that this request is the same as the curl command. But if an error occurs, please check the access token and the values in data, again.
References:
Start/Resume Playback
fetch(url, params)
I'm quite new to curl and I don't know how to convert this to axios statement
curl https://{subdomain}.zendesk.com/api/v2/users/{user_id}/password.json \
-X POST -d '{"password": "newpassword"}' -H "Content-Type: application/json" \
-v -u {email_address}:{password}
I can make this run in postman but I'm not sure to convert it to axios.
Below is my sample axios code but it I get a EOF error
const response = await axios.post(`https://${subdomain}.zendesk.com/api/v2/users/${userId}/password.json`,
{
"password": "12345678"
},
{
auth: {
username: `${email}/token`,
password: ZENDESK_API_KEY
}
})
Passing data from POSTMAN as x-www-form-urlencoded
Key and values are as follows:
data : P1;P2
format : json
Corresponding curl code from POSTMAN
curl --location --request POST 'https://ap-url/id/' \
--header 'content-type: application/x-www-form-urlencoded' \
--data-urlencode 'data=P1;P2' \
How to send the data as x-www-form-urlencoded on HttpClient?
Use https://curl.olsh.me/ for curl commands to C# code
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api-url/id"))
{
var contentList = new List<string>();
contentList.Add($"data={Uri.EscapeDataString("P1;P2")}");
contentList.Add($"format={Uri.EscapeDataString("json")}");
request.Content = new StringContent(string.Join("&", contentList));
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
}
}
The best Pattern is to set a dictionary and send this dictionary data in the post method
var client = _clientFactory.CreateClient("mobile-server");
var data = new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id",
_configuration.GetValue<string>("MobileTop:ClientId")),
new KeyValuePair<string, string>("client_secret",
_configuration.GetValue<string>("MobileTop:ClientSecret")),
};
var response =
await client.PostAsync("api/v2/connect/token", new FormUrlEncodedContent(data));
What is the equivalent axios request for cURL request below?
curl -X POST \
http://159.89.90.5:5000/users/login \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"email": "pwerner#gmail.com",
"password": "password"
}'
I've tried using axios request as follow:
let axiosConfig = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
};
this.$http.post('http://159.89.90.5:5000/users/login', {"email": "pwerner#gmail.com", "password": "password"}, axiosConfig)
.then(response => {
console.log(response)
}).catch(function (error) {
console.log(error);
currentObj.output = error;
});
but I keep getting CORS error because the request doesn't fit with the request I made in cURL above. What am I missing here? if correct like curl request above I'll get email parameter with value pwerner#gmail.com
below is the error I've got when trying to make the request:
but if I use formData instead of using json for the same code, it works, and my server (flask framework) is already provided header: response.headers.add("Access-Control-Allow-Origin", "*") for solving CORS issue