REST Client extension not sending form-data in VSCode - rest

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.

Related

I keep getting error 404 when creating a new document archive

I am testing the Clio API through Postman but I keep getting error 404 when I tried creating a new document. Below is the sample request.
POST https://app.clio.com/api/v4/document_archives.json?fields=id,size,progress,state
Content-Type: application/json
Authorization: Bearer ****
Body
{
"data": {
"items": [
{
"id": 5998571944,
"type": "Folder"
}
]
}
}

How to set up dredd to ignore headers in the assertion

I am using Dredd to test one of my endpoints. I think the test is failling due to the headers, because I'm getting the following expected:
headers:
Content-Type: application/json
body:
[
{
"id": 15107,
"name": "Route Name",
"date": "2022-01-27T09:00:00",
"full_user_name": "Test Admin",
"url": "api/2/users/1/routes/15107"
}
]
statusCode: 200
And the following actual:
statusCode: 200
headers:
date: Wed, 02 Feb 2022 12:54:11 GMT
server: WSGIServer/0.2 CPython/3.6.9
content-type: application/json; charset=utf-8
vary: Accept, Cookie
allow: GET, POST, PATCH, HEAD, OPTIONS
access-control-allow-origin: None
access-control-allow-methods: POST,GET
access-control-allow-headers: Origin,Content-Type,Accept,Accept-Encoding
access-control-allow-credentials: true
x-frame-options: SAMEORIGIN
content-length: 136
bodyEncoding: utf-8
body:
[
{
"id": 15107,
"name": "Route Name",
"date": "2022-01-27T09:00:00",
"full_user_name": "Test Admin",
"url": "api/2/users/1/routes/15107"
}
]
The only difference I see is the headers. Is there any way to ignore those? Like, for instance, content-length header. Maybe I only need to include the auth header in the Open API file. Right now I have specified documentation as follows:
...
securitySchemes:
ApiKey:
type: apiKey
name: Authorization
in: header
description: Authentication token
security:
- ApiKey: []
And running dredd as follows with a python server:
dredd oas-routes.yaml http://127.0.0.1:8000/api/2/users/1/routes --header="Authorization: Token 6b232e1c00bbc8b12d0066a483bf401009fbdb21"
Any idea how to do this correctly? I'm quite new to testing with dredd to be honest.

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.

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

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)

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.