Get more than one header using the metadataHeaders[] query parameter GmailAPI - google-api

When I make a GET http request for the metadataHeaders that only requests one of them like so:
https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From
it works just fine. But my question is how do I go about sending the array of headers that I want [to, from, subject], in my request? So basically, how would I restructure my metadataHeaders query parameter... found here ->
https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From
to also contain From, To, & Subject
I have been trying to figure out how to get these headers for quite a while to no avail. I tried looking at the documentation ( https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get ) but although I know its possible thanks to it, I can't seem to find out how to implement it in my http request. I also tried looking through the stack overflow responses to similar questions but many weren't really useful at all since many of the questions were different from mine, using the oauth library, or in a programming language. All I care for is how to make the http request.

Headers is just an array So you can just add it more then once
metadataHeaders=from&metadataHeaders=to&metadataHeaders=subject
Request:
GET https://gmail.googleapis.com/gmail/v1/users/me/messages/185cf8d12166fc7a?format=metadata&metadataHeaders=from&metadataHeaders=to&key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Resonse:
"payload": {
"partId": "",
"headers": [
{
"name": "From",
"value": "[REDACTED]"
},
{
"name": "To",
"value": "[REDACTED]#gmail.com"
},
{
"name": "Subject",
"value": "Security alert"
},
]
},

Related

Envoy external processing filter: issues with Content-Length Header when processing request body

I'm having a bit of a struggle with using the External Processing filter for Envoy (described HERE. I'm currently using a similar setup to THIS EXAMPLE , where I want to do some processing on both the request and response bodies being sent to an upstream service using a GRPC server in Go.
The issue I'm facing is that when I try to make changes to the request body, meaning using a BodyMutation to send a new body (basically the same JSON but with one field altered as an example) the Content-Length header is missing when the request is forwarded to the upstream service, which causes an error. Yet if I don't alter the body (so no BodyMutation) the header is there as it should be.
Basically, if I try to set the processing response for the request body like so:
bytesToSend := b.RequestBody.Body
resp = &pb.ProcessingResponse{
Response: &pb.ProcessingResponse_RequestBody{
RequestBody: &pb.BodyResponse{
Response: &pb.CommonResponse{
HeaderMutation: &pb.HeaderMutation{
SetHeaders: []*core.HeaderValueOption{
{
Header: &core.HeaderValue{
Key: "Content-Length",
Value: strconv.Itoa(len(bytesToSend)),
},
},
},
},
BodyMutation: &pb.BodyMutation{
Mutation: &pb.BodyMutation_Body{
Body: bytesToSend,
},
},
},
},
},
ModeOverride: &v3.ProcessingMode{
ResponseHeaderMode: v3.ProcessingMode_SEND,
ResponseBodyMode: v3.ProcessingMode_NONE,
},
}
The Content-Length is removed completely. But if I don't set the BodyMutation, like so:
bytesToSend := b.RequestBody.Body
resp = &pb.ProcessingResponse{
Response: &pb.ProcessingResponse_RequestBody{
RequestBody: &pb.BodyResponse{
Response: &pb.CommonResponse{
HeaderMutation: &pb.HeaderMutation{
SetHeaders: []*core.HeaderValueOption{
{
Header: &core.HeaderValue{
Key: "Content-Length",
Value: strconv.Itoa(len(bytesToSend)),
},
},
},
},
},
},
},
ModeOverride: &v3.ProcessingMode{
ResponseHeaderMode: v3.ProcessingMode_SEND,
ResponseBodyMode: v3.ProcessingMode_NONE,
},
}
Then the Content-Length is present, and the HeaderMutation does set it to whatever I set it to (in the example it's just the length of the body again).
I'm not entirely clear on the behaviour here. Is there something in particular I should be looking out for, either in this code or Envoy's configuration?
Any pointers would be greatly appreciated.
EDIT: Seems like the behaviour here is: when the body is mutated, it switches to chunked transfer encoding, therefore removing the content length. Makes sense, but then if the upstream service requires a content length it causes issues. The Header Mutation is ignored, so computing the content length manually and sending it doesn't seem to work.

Problem with create team with Microsoft Graph Api

I have a problem with creating teams using the Microsoft Graph Api. I can get/create groups but when I try to get/create teams I get an error. I'm using postman and the group has owners and members, just as the documentation of MS, also has the permissitions it asks for groups. If somebody can help me, cause I look everywhere for a same error but no found it.
PUT https://graph.microsoft.com/v1.0/groups/{id}/team
Headers: Authorization: bearer token and content-type: json
Body is
{
"memberSettings": {
"allowCreateUpdateChannels": true
},
"messagingSettings": {
"allowUserEditMessages": true,
"allowUserDeleteMessages": true
},
"funSettings": {
"allowGiphy": true,
"giphyContentRating": "strict"
}
}
I always get the same error
{
"error": {
"code": "BadGateway",
"message": "Failed to execute backend request.",
"innerError": {
"request-id": "45eeba8a-9d35-45e8-b42e-c60da7a47dde",
"date": "2020-01-23T21:55:44"
}
}
}
According to the Graph API docs for this, you're not calling the correct endpoint to create a new Team. It should be
POST https://graph.microsoft.com/beta/teams
and a payload similar to
Content-Type: application/json
{
"template#odata.bind": "https://graph.microsoft.com/beta/teamsTemplates('standard')",
"displayName": "My Sample Team",
"description": "My Sample Team’s Description",
"owners#odata.bind": [
"https://graph.microsoft.com/beta/users('userId')"
]
}
Note that it's slightly different, as per the docs, whether you're using delegated versus application permissons.

GoCardless API using Classic ASP

I'm creating the following request in vbscript and sending to the gocardless sandbox:
url="https://api-sandbox.gocardless.com/"
typ="GET"
Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
xml.Open typ, url, False
xml.setRequestHeader "Authorization", "Bearer " & GCAccessToken
xml.SetRequestHeader "GoCardless-Version", "2015-07-06"
xml.SetRequestHeader "Accept","application/json"
xml.SetRequestHeader "Content-Type", "application/json"
xml.Send
GetGC = xml.responseText
Set xml = Nothing
The response I always get despite any tweaks I do is:
{"error":{"message":"not found","errors":[{"reason":"not_found","message":"not found"}],"documentation_url":"https://developer.gocardless.com/api-reference#not_found","type":"invalid_api_usage","request_id":"0AA4000DECCD_AC121CEB1F90_5BE18701_19AD0009","code":404}}
Any help would be appreciated. Have successfully done similar for Stripe but now need to use GC.
If you read the response from the API
{
"error": {
"message": "not found",
"errors": [{
"reason": "not_found",
"message": "not found"
}
],
"documentation_url": "https://developer.gocardless.com/api-reference#not_found",
"type": "invalid_api_usage",
"request_id": "0AA4000DECCD_AC121CEB1F90_5BE18701_19AD0009",
"code": 404
}
}
The error appears to be a HTTP status code (as is common with RESTful APIs) - 404 Not Foundlooking at the documentation link provided in the response;
404
Not Found. The requested resource was not found or the authenticated user cannot access the resource. The response body will explain which resource was not found.
So the issue could be;
You have failed to authenticate using the token in the code provided.
You authenticated but don't have permission to access the resource.
The resource you are looking for does not exist.
In this particular instance, I would suggest it is because the resource doesn't exist as the code doesn't specify a resource, only the base URL of the API which won't constitute an API endpoint you can interact with.
Looking at the documentation it's clear you need to provide a valid endpoint in the URL, at the time of writing there are 15 core endpoints to interact with along with 2 helper endpoints.
For example, a create payment request/response would look like;
POST https://api.gocardless.com/payments HTTP/1.1
{
"payments": {
"amount": 100,
"currency": "GBP",
"charge_date": "2014-05-19",
"reference": "WINEBOX001",
"metadata": {
"order_dispatch_date": "2014-05-22"
},
"links": {
"mandate": "MD123"
}
}
}
HTTP/1.1 201 (Created)
Location: /payments/PM123
{
"payments": {
"id": "PM123",
"created_at": "2014-05-08T17:01:06.000Z",
"charge_date": "2014-05-21",
"amount": 100,
"description": null,
"currency": "GBP",
"status": "pending_submission",
"reference": "WINEBOX001",
"metadata": {
"order_dispatch_date": "2014-05-22"
},
"amount_refunded": 0,
"links": {
"mandate": "MD123",
"creditor": "CR123"
}
}
}
Unfortunately, the code sample provided in the question doesn't really do anything so it's difficult to suggest what you are trying to do. In conclusion, I would suggest re-visiting the documentation for the API and look through the samples provided.

Wiremock Standalone with Dynamic response

I have a standalone instance of Wiremock server. The mappings are stored as json files under the mappings folder. I have a POST request that needs to return a dynamic ID(integer) in the response. Is there a way to configure this in the json file?
To make the above examples work, I had to run the standalone jar with the --global-response-templating. Then I saw, for example, {{now}} working which is what I wanted. Not sure if the documentation specifies this -- I tried the always-useful --help.
In WireMock there are a number of response template helper functions for generating random strings. In the below example I'm using the one for generating a UUID, but several other options exist.
Mapping file: dynamic_id.json
{
"request": {
"method": "POST",
"url": "/dynamic_id"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"body": "{{randomValue type='UUID'}}",
"transformers": ["response-template"]
}
}
Using an empty POST http://wiremock/dynamic_id will return an id similar to: c2e6bf32-c9a3-45c0-b988-94fad04cc7a2.
Start WireMock:
java -jar wiremock-standalone-2.18.0.jar --port 8181 --verbose --local-response-templating
This seems like a perfect use-case for OpenTable's Wiremock Body Transformer.
It can be easily integrated with the Standalone Server like this:
java -cp "wiremock-body-transformer-1.1.6.jar:wiremock-2.3.1-standalone.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.opentable.extension.BodyTransformer
And allows you to easily specify a dynamic variable that you would want to match in the response.
Here is an example to get a random integer without having to specify anything in the request, however if you need to match a specific variable in the request to the response, then that is also very doable with this extension and numerous examples can be found in the readme.
{
"request": {
"method": "POST",
"urlPath": "/transform",
},
"response": {
"status": 200,
"body": "{\"randomInteger\": \"$(!RandomInteger)\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["body-transformer"]
}
}
As #Jeff mentions, If you are running it as a stand-alone process, you need to add this flag --global-response-templating. This will apply templating to each and every reponse. However, few of your responses may be jsut plain json with no templating required.
In that case use --local-response-templating. and add this field inside reponse json:
response:{
"transformers": ["response-template"]
}

MailChimp API 3.0 batch request returns 400 Invalid Resource error

I've been trying to use the batch endpoint of MailChimp API (version 3.0) to subscribe new users to a list, but can't make it work.
Here is the request:
POST /3.0/batches
{
"operations": [
{
"method" : "POST",
"path" : "lists/c852ce5c86/members",
"body": "{\"email_address\":\"email#domain.tld\", \"status\":\"subscribed\"}"
}
]
}
The request seems ok cause I get a 200 response:
{
"id": "49abca6ef3",
"status": "finished",
"total_operations": 1,
"finished_operations": 1,
"errored_operations": 1,
"submitted_at": "2015-09-21T18:11:16+00:00",
"completed_at": "2015-09-21T18:11:23+00:00",
"response_body_url": "https://mailchimp-api-batch.s3.amazonaws.com/49abca6ef3-response.tar.gz?..."
}
However, as you can see, the only operation in my batch is errored.
Here is the response_body_url for this operation:
[{
"status_code":400,
"operation_id":null,
"response":"{
\"type\":\"http://kb.mailchimp.com/api/error-docs/400-invalid-resource\",
\"title\":\"Invalid Resource\",
\"status\":400,
\"detail\":\"The resource submitted could not be validated. For field-specific details, see the 'errors' array.\",
\"instance\":\"\",
\"errors\":[{
\"field\":\"\",
\"message\":\"Schema describes object, NULL found instead\"
}]
}"
}]
which is not very helpful :(
Note that if I directly hit POST lists/c852ce5c86/members with {"email_address":"email#domain.tld", "status":"subscribed"} payload, it's working properly.
That was actually a bug in the mailchimp API. After reaching them they quickly fixed it.

Resources