Wiremock to remove additional data (meta-data) in response - wiremock

I am trying to post a file in wiremock and configure the file in mapping like below. And in the response I am seeing Content-Type, Content-Disposition etc, is there a way to disable this?
POST file
http://localhost:8080/__admin/files/some.json
used form-data to upload the file
contents of some.json
{
"user": "xxx"
}
To create mapping
http://localhost:8080/__admin/mappings
{
"request": {
"method": "GET",
"url": "/some"
},
"response": {
"status": 200,
"bodyFileName": "some.json",
"headers": {
"Content-Type": [
"application/json;charset=UTF-8"
]
}
}
}
To check the api
http://localhost:8080/some
Response:
----------------------------228585284577179878202292
Content-Disposition: form-data; name="file"; filename="some.json"
Content-Type: application/json
{
"user": "xxx"
}
----------------------------228585284577179878202292--
Like you see, there are additional content like below to the actual response. Wanted to disable the below. How to do this?
----------------------------228585284577179878202292
Content-Disposition: form-data; name="file"; filename="some.json"
Content-Type: application/json
----------------------------228585284577179878202292--

Instead of form-data choose binary while uploading the file. Then it return only the actual response...
{
"user": "xxx"
}

If you put a file by HTTP request to wiremock instance, as #Minisha said, add 'Content-type': 'binary' header to your put request. Here is an example with python requests:
headers = {'Content-type': 'binary'}
requests.put(url=mock_wire_files_url_file_name, data=open(your_file, 'rb'), headers=headers)

Related

VSCode httpClient plugin showing Header name must be a valid HTTP token

I have this API call in the VSCode RESTAPI caller plugin:
###
POST {{endpoint}}/snapcenter/SnapCenterInventory/account/{{accountId}} {{#protocol}}
content-type: application/json
Authorization: {{token}}
{
"Server": "10.10.10.10",
"ApplicationCount": 10,
"User": "admin",
"Password": "foo",
"Port": 3000,
"RoleName": "bar"
}
###
I have all the variables defined, working fine in GET cases.
However for POST/PATCH/PUT, it's giving this error:
Header name must be a valid HTTP token ["{"]
Not able to figure out what I am missing here.
Any help will be highly appreciated.
Thanks,
Pradip
I think I can able to find it out. I need to keep a newline b/w the header and the content.
This works:
POST {{endpoint}}/servers/account/{{accountId}} {{#protocol}}
Content-Type: application/json
Authorization: {{token}}
{
"k1": "v5",
"k2": 110,
"k3": true,
"k4": ["v1", "v2", "v3", "v4", "v5"],
"k5": {
"k51": "v5",
"k52": 2
},
"k6": {
"k61": true
}
}
Mind the gap b/w the content and the header.

REST Client extension not sending form-data in VSCode

I'm attempting to POST Authenticate using REST Client in vscode, but it isn't accepting my form-data. This is the http code from postman which appears to match specs on https://marketplace.visualstudio.com/items?itemName=humao.rest-client but I don't understand why it doesn't like my credentials when submitting from vscode. Any help or pointers greatly appreciated.
Request:
POST /myservicestack/authenticate/credentials HTTP/1.1
Host: services.mydomain.com
Accept: application/json
cache-control: no-cache
Postman-Token: cdax7d61-8d8b-4f3q-b45v-74a240f33693
Content-Disposition: form-data; name="UserName"
myemail#address.com
Content-Disposition: form-data; name="Password"
My Password!
Content-Disposition: form-data; name="RememberMe"
true
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Response:
{
"responseStatus": {
"errorCode": "ValidationException",
"message": "Validation failed: \r\n -- 'User Name' should not be empty.\r\n --
'Password' should not be empty.",
"errors": [
{
"errorCode": "NotEmpty",
"fieldName": "UserName",
"message": "'User Name' should not be empty.",
"meta": {
"PropertyName": "User Name"
}
},
{
"errorCode": "NotEmpty",
"fieldName": "Password",
"message": "'Password' should not be empty.",
"meta": {
"PropertyName": "Password"
}
}
]
}
}
The tool here is a red-herring and you should be checking what data format the server endpoint is expecting its data in. Typically REST services are expecting requests as JSON, but that is not universally true.
So for example (including both header and body):
POST https://example.com/comments HTTP/1.1
content-type: application/json
{
"UserName": "myemail#address.com",
"Password": "My Password!",
"RememberMe": true
}
If you want to see what the server may be receiving, then you can check against a service such as https://requestbin.com/ - just don't include any sensitive data.

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

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.

How does the body of the request look like when making a call to gmail api for sending an email with attachment (multipart)?

Google documentation gave an example as below:
POST /upload/gmail/v1/users/userId/messages/send?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: number_of_bytes_in_entire_request_body
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
--foo_bar_baz
Content-Type: message/rfc822
Email Message data
--foo_bar_baz--
If the request succeeds, the server returns the HTTP 200 OK status code along with any metadata:
HTTP/1.1 200
Content-Type: application/json
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
Can someone make a sample request body by looking at the above example?
I need to send an email with attachment.
Based from SO related post, the body request can be something like this:
var mail = [
'Content-Type: multipart/mixed; boundary="foo_bar_baz"\r\n',
'MIME-Version: 1.0\r\n',
'From: sender#gmail.com\r\n',
'To: receiver#gmail.com\r\n',
'Subject: Subject Text\r\n\r\n',
'--foo_bar_baz\r\n',
'Content-Type: text/plain; charset="UTF-8"\r\n',
'MIME-Version: 1.0\r\n',
'Content-Transfer-Encoding: 7bit\r\n\r\n',
'The actual message text goes here\r\n\r\n',
'--foo_bar_baz\r\n',
'Content-Type: image/png\r\n',
'MIME-Version: 1.0\r\n',
'Content-Transfer-Encoding: base64\r\n',
'Content-Disposition: attachment; filename="example.png"\r\n\r\n',
pngData, '\r\n\r\n',
'--foo_bar_baz--'
].join('');
var response = UrlFetchApp.fetch(
"https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media", {
method: "POST",
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken(),
"Content-Type": "message/rfc822",
},
muteHttpExceptions: true,
payload: mail
});
Here's also an example code from digital inspiration written by Amit Agarwal in Google Appscript.
This example shows how you can easily send email messages with file attachment using Gmail API.

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.