I try convert this curl code to GoogleAppsScript
curl --location --request POST 'http://mpstats.io/api/wb/get/category?d1=2020-07-13&d2=2020-08-11&path=%D0%96%D0%B5%D0%BD%D1%89%D0%B8%D0%BD%D0%B0%D0%BC/%D0%9E%D0%B4%D0%B5%D0%B6%D0%B4%D0%B0'
--header 'X-Mpstats-TOKEN: 5edfe8277ff2a5.941835169784f269a9244f13350db36eedb814d1'
--header 'Content-Type: application/json'
--data-raw '{"startRow":0,"endRow":100,"filterModel":{"id":{"filterType":"number","type":"equals","filter":13495594,"filterTo":null}},"sortModel":[{"colId":"revenue","sort":"desc"}]}'
but part of code : "startRow":0,"endRow":100 = don't work
I try this code in Google Apps Script
let optionMpStats ={
headers: {
'X-Mpstats-TOKEN': token,
'Content-Type': 'application/json'
},
payload: JSON.stringify({
'startRow':0,
'endRow':10,
})
};
Related
curl -k --request POST --url https://vault:8200/v1/sys/policy/policy-test --header 'Authorization: Bearer <Token>' --header 'Content-Type: application/json' --data '"policy":"{""path\\"" //*\" { \n capabilities = [\"create\",\"read\",\"list\",\"update\"]}}}"'
While trying this command, I am getting this error:
{"errors":["error parsing JSON"]}
Could anyone help with syntax for Vault policy here?
It's easy to visualize (and fix the errors) if you use the "#-" --data option and a here-document, like shown following:
curl -k --request POST --url https://vault:8200/v1/sys/policy/policy-test --header 'Authorization: Bearer ' --header 'Content-Type: application/json' --data #- <<EOF
{
"policy":
{
"path \"/*\"
{
capabilities = [\"create\", \"read\", \"list\", \"update\"]
}"
}
}
EOF
TLDR
For my API project I require to send a POST request to MRP to update some of the fields.
What I am currently struggling with is the ability to parse in the parameters as data, every time I send the request I get a code 400.
Full Explanation
MRPeasy has a documentation page which I am using heavily for this project as that is the only source of info.
In that article they give 2 examples, 1 for GET and 1 for POST, I have no issues whatsoever with the get request, it works perfectly fine. However, the POST does not, they are as follows:
curl -X "GET" "https://app.mrpeasy.com/rest/v1/items" \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
curl -X "PUT" "https://app.mrpeasy.com/rest/v1/items/5" \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx' \
-d '{"selling_price": "2.54"}'
Below is my representation of the above code in python:
```python
url = "https://app.mrpeasy.com/rest/v1/manufacturing-orders/69"
headers = {
"Content-Type": "application/json",
"api_key": my_api_key,
"access_key": my_access_key
}
print(requests.get(url, headers=headers).json()["custom_3338"])
url = "https://app.mrpeasy.com/rest/v1/manufacturing-orders/69"
headers = {
"Content-Type": "application/json",
"api_key": my_api_key,
"access_key": my_access_key
}
data = json.dumps({"custom_3338": "1654642800.0000000000"})
print(requests.post(url, headers=headers, data=data).status_code)
```
Regarding the data variable, I have tried all of the below:
'{"custom_3338": "1654642800.0000000000"}'
{"custom_3338": "1654642800.0000000000"}
{"due_date": "1654642800.0000000000"}
{"quantity": 3}
'{"quantity": 3}'
I hope that is sufficient information. If you need anything else from me, please let me know, I'll be more than happy to provide.
Many Thanks,
Greg
P.S. This is my first post so I apologise if I didn't follow some rules or best practices.
You can create a json File contains the data you want to send as data and send the request with the JSON file itself.
JSON
{"custom_3338": "1654642800.0000000000"}
curl
curl -X POST \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
--data "#./processGroup.json" \
https://app.mrpeasy.com/....
Or easier actually, pass the data directly in the body :
curl -X POST \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
--data '{"custom_3338": "1654642800.0000000000"}' \
https://app.mrpeasy.com/....
I have been converting this curl syntax
curl -L -X POST 'https://driver-vehicle-licensing.api.gov.uk/vehicleenquiry/v1/vehicles' \-H 'x-api-key: REPLACE WITH YOUR API KEY' \-H 'Content-Type: application/json' \-d '{"registrationNumber": "TE57VRN"}'
into Swift with Alamofire and have got this syntax
guard let url = URL(string: "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles") else { return }
let headers : HTTPHeaders = [
"x-api-key": "mykey",
"Content-Type": "application/json"
]
let params: Parameters = [
"registrationNumber" : "myreg"
]
AF.request(url, method: .post, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: headers).responseJSON(completionHandler: { response in
print(response)
})
However, I have got the output
success({
errors = (
{
code = ENQ103;
detail = "Invalid format for field - vehicle registration number";
status = 400;
title = "Bad Request";
}
);
Please could someone explain what I have done wrong. Thanks
The API expects the parameters to be sent as JSON, but you are sending as URL encoded.
Try using JSONParameterEncoder.default as the encoder instead of URL encoding.
Check Usage from the docs: POST request with JSON Parameters
let parameters: Parameters = [
"registrationNumber" : "myreg"
]
AF.request(url, method: .post, parameters: parameters, encoder: JSONParameterEncoder.default)
You have a working curl command, small (or big) tip: AlamoFire can print the request as a curl command!
In "one step":
AF.request(url, method: .post,
parameters: params,
encoding: URLEncoding(destination: .httpBody),
headers: headers)
.cURLDescription(calling: { curlCommand in
print("curlCommand1: \(curlCommand)")
}).responseJSON(completionHandler: { response in
print(response)
})
In "two steps", if you prefer to separate your calls and not chain them all:
let request = AF.request(url, method: .post,
parameters: params,
encoding: URLEncoding(destination: .httpBody),
headers: headers)
request.cURLDescription { curlCommand in
print("curlCommand2: \(curlCommand)")
}
request.responseJSON(completionHandler: { response in
print(response)
})
And you should have output:
curl -v \
-X POST \
-H "Accept-Encoding: br;q=1.0, gzip;q=0.9, deflate;q=0.8" \
-H "User-Agent: Alamofired/1.0 (...; build:1; iOS 14.5.0) Alamofire/5.4.0" \
-H "Accept-Language: en;q=1.0, fr-US;q=0.9" \
-H "Content-Type: application/json" \
-H "x-api-key: mykey" \
-d "registrationNumber=myreg" \
"https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles"
Now, if we compare with yours:
curl \
-L \
-X POST \
'https://driver-vehicle-licensing.api.gov.uk/vehicleenquiry/v1/vehicles' \
-H 'x-api-key: REPLACE WITH YOUR API KEY' \
-H 'Content-Type: application/json' \
-d '{"registrationNumber": "TE57VRN"}'
I added some \ to make it sill valid...
We shouldn't care about the order, the "Accept-Language", "User-Agent", "Accept-Encoding" headers that are added with Alamofire shouldn't be an issue.
Try your command with no -L parameter maybe to check if it's "important" (if it's --location).
And then, the difference is with the data paramater (-d). Once is JSON, the other is URL encoded.
Then, let's see your code, and URLEncoding(destination: .httpBody)
might be the culprit. Using JSONEncoding.default instead will work.
Now, if you didn't have had the curl command, just by seeing your code, I spotted it. If you think about it:
You are saying there that you will provide the parameter in body following the JSON protocol.
let headers : HTTPHeaders = ["Content-Type": "application/json"]
And later, you used URL Encoding protocol.
URLEncoding(destination: .httpBody)
Doesn't make sense, right? You're contradicting yourself.
I am looking for a way to generate token using login/password on reportportal.
Pretty sure, there should be a way using a API call. I am just not able to find it.
So given, (project name, user, password), I should get a token that I can use for making other API calls.
Thanks.
Get API token
Before getting API token, you have to generate it.
If it's already generated (you have logged in with this user), this endpoint will return you existing API token:
GET /sso/me/apitoken
Example:
curl -X GET --header 'Accept: application/json' --header 'Authorization: bearer 4f73871b-e477-4f49-b1bd-805b24201fe0' 'http://web.demo.reportportal.io/uat/sso/me/apitoken'
Response:
{
"access_token": "b1debc0a-d47d-492f-aa7c-3e2e0fb96332",
"token_type": "bearer",
"scope": "api"
}
Pay attention, that bearer in 1st (curl) request has scope UI and bearer in response has scope API.
UI token expires according to server config and TimeToLive for user session.
API token has endless TTL
** If GET requests returns you error, then you need to generate token.
Generate API token
POST sso/me/apitoken
Example:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: bearer 4f73871b-e477-4f49-b1bd-805b24201fe0' 'http://web.demo.reportportal.io/uat/sso/me/apitoken?authenticated=true'
Response:
{
"access_token": "4e76e31e-0250-4e5e-ba66-90105dd014bb",
"token_type": "bearer",
"scope": "api"
}
(!) Tokens are public. Used from public account at demo instance http://web.demo.reportportal.io
Make a post to:
/uat/sso/oauth/token?grant_type=password&password=***password***&username=***username***
Set basic authentication with credentials (username: ui and password: uiman) to get the accesstoken for scope UI, use this token to fetch apitoken.
How can I upload a 360 degree image to Google map street view. (I know to take 360 degree photo and upload from street view app).
You may follow this documentation on how to upload photos using curl.
Creating a photo requires three separate calls. The first call will return an upload URL, which is used in the second call to upload the photo bytes. After the photo bytes are uploaded, the third call uploads the metadata of the photo and returns the photo ID.
Request an Upload URL
$ curl --request POST \
--url 'https://streetviewpublish.googleapis.com/v1/photo:startUpload?key=YOUR_API_KEY' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
--header 'Content-Length: 0'
Upload the photo bytes to the Upload URL
$ curl --request POST \
--url 'UPLOAD_URL' \
--upload-file 'PATH_TO_FILE' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Upload the metadata of the photo
$ curl --request POST \
--url 'https://streetviewpublish.googleapis.com/v1/photo?key=YOUR_API_KEY' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
"uploadReference":
{
"uploadUrl": "UPLOAD_URL"
},
"pose":
{
"heading": 105.0,
"latLngPair":
{
"latitude": 46.7512623,
"longitude": -121.9376983
}
},
"captureTime":
{
"seconds": 1483202694
},
}'
There are also client libraries available which you may use to avoid the need to manually set up HTTP requests and parse the responses.