Google Apps Script doesn't recognize "Host" header in HTTP POST request? - rest

I'm trying to query ArcGIS Rest API from Google Apps script. Building the request in Postman works perfectly fine, but when I get the code into apps script, I'm having trouble that I cant seem to figure out. Here's the code:
function callEsri () {
var url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query"
var params =
{
"async": true,
"crossDomain": true,
"url": "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query",
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "PostmanRuntime/7.20.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "[TOKEN]",
"Host": "services3.arcgis.com",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "125",
"Connection": "keep-alive",
"cache-control": "no-cache"
},
"data": {
"f": "json",
"where": "CITY_JUR LIKE '%Los Angeles%'",
"outSr": "4326",
"outFields": "TRL_NAME,ELEV_FT,CITY_JUR,PARK_NAME,FID"
}
}
var response = UrlFetchApp.fetch(url, params);
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);
}
The Error I am getting is: Execution failed: Attribute provided with invalid value: Header:Host (line 28, file "Code")
Any reason why Google is not recognizing this and is there a way to fix this? Any help/advice is greatly appreciated.

As #TheMaster has already noted in the comments. You are already specifying the Host in the url.
Also you can take a look at the official documentation of URLFetchApp.
And in case you want more information in the head here you have the mozilla documentation on that header and also the RFC specifying the Host header.

Related

How to send sms to user using msg91 in flutter?

I have written the api call like this:
var response = await http.post(
Uri.parse("http://api.msg91.com/api/v2/sendsms"),
headers: {
"Content-Type": "application/json",
"authkey": "API key"
},
body: jsonEncode({
"sender": "note",
"route": "4",
"country": "91",
"flash": 1,
"sms":
{"message": "Message1", "to": "9999999999"}
}),
);
But this giving me error : {type: error, message: Invalid content type.Please send data in formdata,application/xml,application/json format, code: }
How can I correct this?
Try removing theses
"Content-Type": "application/json"
or replace it with
"Content-Type": "application/xml"
You should use multipart/form-data as content-type ->
"Content-Type": "multipart/form-data"

How to send large json data inside the body of http post

I need to send a large object with my post method. but it kept giving me error.
Future getResults() async {
var res = await http.post('$SERVER_IP/api/anything/search',
headers: {'Authorization': token, "Accept": "application/json"},
body: {
"name": "",
"type": "",
"organization": "",
"state": "CA",
"appliesTo": {
"Camp": "true",
"Fees": "false",
"Lessons": "false",
},
}).catchError((e) => print({"error": e}));
return json.decode(res.body);
}
I realized that body only accept Map<String, String>. So I added json.encode to my "appliesTo" object.
Future getResults() async {
var res = await http.post('$SERVER_IP/api/anything/search',
headers: {'Authorization': token, "Accept": "application/json"},
body: {
"name": "",
"type": "",
"organization": "",
"state": "CA",
"appliesTo": json.encode({
"Camp": "true",
"Fees": "false",
"Lessons": "false",
}),
}).catchError((e) => print({"error": e}));
return json.decode(res.body);
}
After that it worked and I got a returned data. But my server was ignoring whole "appliesTo" object. So I didn't get the expected data. it's not a problem in my server. I tested it with postman. this flutter http post is not sending proper json body.
So my question is how to attatch large object to the body? was using json.encode at the middle of the object wrong? what is the proper way of doing it? can anyone help?
PS: I wraped the whole Map with json.encode and it gave me error
The documentation of the http package states that
If body is a Map, it's encoded as form fields using encoding. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
If you want to send JSON data instead, you have to encode the body manually. Since the content type defaults to text/plain if body is a String, you also have to set the Content-Type header explicitly.
http.post('$SERVER_IP/api/anything/search',
headers: {
'Authorization': token,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: json.encode({
"name": "",
"type": "",
"organization": "",
"state": "CA",
"appliesTo": {
"Camp": "true",
"Fees": "false",
"Lessons": "false",
},
}))

Sending Transactional Email via SendGrid with jQuery & Google Tag Manager

I'm trying to send an email using a google tag manager custom html tag when certain events are triggered.
This is the code (XXXXX for omission where needed)
<script>
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.sendgrid.com/v3/mail/send",
"method": "POST",
"headers": {
"authorization": "Bearer XXXXXXXXXXXXX",
"content-type": "application/json"
},
"processData": false,
"data": "{\"personalizations\":[{\"to\":[{\"email\":\"XXXXXXXXXX\",\"name\":\"John Doe\"}],\"dynamic_template_data\":{\"verb\":\"yes\",\"adjective\":\"test\",\"noun\":\"\",\"currentDayofWeek\":\"\"},\"subject\":\"Hello, World!\"}],\"from\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"reply_to\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"template_id\":\"XXXXXXXXXXXXXXX"}"
}
jQuery.ajax(settings).done(function (response) {
console.log(response);
});
</script>
Now This block (minus the script tags) runs when using the built in send grid "try it out" feature. Why will this not work from GTM?
Sounds like this code might be getting added and run prior to jQuery being ready. Try the following update to the code:
<script>
jQuery(function() {
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.sendgrid.com/v3/mail/send",
"method": "POST",
"headers": {
"authorization": "Bearer XXXXXXXXXXXXX",
"content-type": "application/json"
},
"processData": false,
"data": "{\"personalizations\":[{\"to\":[{\"email\":\"XXXXXXXXXX\",\"name\":\"John Doe\"}],\"dynamic_template_data\":{\"verb\":\"yes\",\"adjective\":\"test\",\"noun\":\"\",\"currentDayofWeek\":\"\"},\"subject\":\"Hello, World!\"}],\"from\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"reply_to\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"template_id\":\"XXXXXXXXXXXXXXX"}"
}
jQuery.ajax(settings).done(function (response) {
console.log("SendGrid Response:", response);
});
});
</script>
Try that and see if you see anything in the console labeled "SendGrid Response:". That'll definitively let us know if this script is running at all.

Wiremock - How can I apply response templating to the header name?

I am trying to provide a MOCK service that takes a headerName and value from the query and returns it as a (dynamic) header with the response. I am using the following response definition:
"response" : {
"status" : 200,
"statusMessage": "OK",
"headers" : {
"Content-Type" : "application/json",
"{{request.query.debugHeader}}" : "{{request.query.debugHeaderValue}}"
},
"jsonBody" : {
"headerSent": "{{request.query.debugHeader}} {{request.query.debugHeaderValue}}"
},
"transformers": ["response-template"],
"base64Body" : ""
}
The header value is correctly evaluated and put into the response template, however I can't get the header name to be taken from the request.
When sending a request:
http://localhost:8090/example?debugHeader=name&debugHeaderValue=value
The result headers I get back are:
HTTP/1.1 200 OK
Content-Type: application/json
{{request.query.debugHeader}}: value
However I want {{request.query.debugHeader}} to be replaced with the actual request parameter value ("name" in the example above).
Any ideas?
Thanks in advance
Alex
This is supported in WireMock.Net.
The request which you need to specify looks like this:
{
"Guid": "90356dba-b36c-469a-a17e-669cd84f1f05",
"Priority": 0,
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/trans",
"IgnoreCase": false
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"BodyDestination": "SameAsSource",
"Body": "{\"msg\": \"Hello world : {{request.path}}\" }",
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json",
"Transformed-Postman-Token_{{request.path}}": "token is {{request.headers.Postman-Token}}"
}
}
}
This will add the transformed header Transformed-Postman-Token_{{request.path}} in the response.
Presently this type of variability is not part of the out-of-the-box WireMock application and would have to be custom added. In the WireMock documentation the section Extending WireMock and in particular the part on Transforming Responses.

Wiremock CORS not working

I've been used wiremock effectively for some time now, and I wanted to enable CORS access to the mocked APIs.
I've tried setting Access-Control-Allow-Origin: * and other headers in the response header, both to no avail.
Here's an example of a mapping that I have:
{
"request": {
"method": "POST",
"urlPattern": "/api/v2/general/login[\\/\\&\\?]*",
"bodyPatterns": [{
"equalToJson": "{\"password\":\"password\",\"username\":\"john#cougar.com\"} ",
"jsonCompareMode": "LENIENT",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
}]
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "*",
"Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding",
"X-Content-Type-Options" : "nosniff",
"x-frame-options" : "DENY",
"x-xss-protection" : "1; mode=block"
},
"bodyFileName": "/login_response_johncougar.json"
}
}
What am I doing wrong here that's causing CORS to not work?
Thanks in advance.
I have just managed to fix this issue. Actually solution was here already Adding headers to Jetty in Wiremock.
Because your browser sends a CORS preflight request before making any actual request, you will need to set up your wiremock to stub the OPTIONS request and send back headers.
For example,
Access-Control-Allow-Origin = "*",
Access-Control-Allow-Headers: "content-type",
Access-Control-Allow-Methods = "POST, GET".
Access-Control-Allow-Headers's value has to be the same values the Access-Control-Request-Headers header contained Request header field Access-Control-Allow-Headers is not allowed by itself in preflight response.
All your responses have to send back header "Access-Control-Allow-Origin": "*" as well.
Here is a sample and this works
{
"request":
{
"urlPattern": "/country/([a-z]*)",
"method": "GET"
},
"response":
{
"status": 200,
"headers":
{
"Content-Type" : "application/json",
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "*",
"Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding",
"X-Content-Type-Options" : "nosniff",
"x-frame-options" : "DENY",
"x-xss-protection" : "1; mode=block"
},
"body": "{ \"statusCode\" : \"S1000\", \"statusDescription\" : \"Success\", \"content\" : [ { \"id\" : \"1111\", \"name\" : \"aaaa\"}, { \"id\" : \"2222\", \"name\" : \"asd\" } ] }"
}
}
Use this as it is, wiremock is peculiar when it comes to spacing, here i have used a single space instead of tab, hope it helps.
I had the same problem. After a long time search without finding the solution I started to play with the groovy file and finally I found the solution.
You just need to add each header in header() method. this will solve the problem. So your sample groovy contract will be like this:
{
"request": {
"method": "POST",
"urlPattern": "/api/v2/general/login[\\/\\&\\?]*",
"bodyPatterns": [{
"equalToJson": "{\"password\":\"password\",\"username\":\"john#cougar.com\"} ",
"jsonCompareMode": "LENIENT",
"ignoreArrayOrder": true,
"ignoreExtraElements": true
}]
},
"response": {
"status": 200,
"headers": {
header("Content-Type": "application/json"),
header("Access-Control-Allow-Origin": "*"),
header("Access-Control-Allow-Methods": "*"),
header("Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding"),
header("X-Content-Type-Options": "nosniff"),
header("x-frame-options": "DENY"),
header("x-xss-protection": "1; mode=block")
},
"bodyFileName": "/login_response_johncougar.json"
}
}
I hope it will solve your problem (Actually it will be useful if you use groovy contracts).
You can disable cors by adding --enable-stub-cors flag
Standalone example :
java -jar wiremock-jre8-standalone-2.31.0.jar --port 8081 --enable-stub-cors
Docker example :
docker run -it --rm -p 8081:8080 --name wiremock-yadoms -v $PWD:/home/wiremock wiremock/wiremock --enable-stub-cors
For Angular developers:
first Install wiremock with following command:
npm install wiremock
Then:
"scripts": {
"ng": "ng",
"start": "ng serve",
"wiremock": "wiremock --root-dir ./wiremock --port 8080 --enable-stub-cors true --enable-browser-proxying",
...
}