Response tag with URL parameter - rest

Is there any way in an Insomnia request to use the response body of a sub-request in the request body, and at the same time specify a URL parameter for that sub-request?
Consider an API with endpoints like these:
[GET] _.base_url/customers/{id}
[GET] _.base_url/products/{id}
[POST] _.base_url/invoices
When POSTing to the invoices endpoint, the API expects a JSON body along these lines (highly simplified, of course):
{
"date": "2022-03-31",
"currency": "USD",
"customer": {
"id": 123,
"name": "Something Corp.",
"address": "Here, there and everywhere"
},
"items": [
{
"id": 456,
"description": "Thingamajig",
"price": 11500
},
{
"id": 789,
"description": "Doodad",
"price": 23900
}
]
}
That is, it expects the customer field and each of the items fields to be full objects, not just IDs. (And yes, that’s stupid and not RESTful, but I don’t control the API.)
The proper way to retrieve such objects would obviously be to call the two GET endpoints with the IDs as part of the URL, and then using the responses as response tags in the POST request – but I cannot find a way to do this.
There’s a lengthy GitHub discussion on per-request veriables which has so far not produced any results, but even the suggested PR in that thread doesn’t seem like it would support defining the URL parameters when calling the request, rather than in the request itself.
Is there some way to achieve this in Insomnia?

Related

REST API filter options in response body

I'm trying to display the remaining possible filter options, for a REST endpoint, based on the already set filters.
Is there some kind of best practice, on how to design a REST API, to let the client know about the remaining options?
Something like this came to mind.
{
"count": 131,
"next": "2",
"previous": null,
"filters": {
"status": [
1,
2,
99
],
"...": [
"..."
]
},
"results": [
{
"id": 1,
"status": 1,
"...": ".."
},
{
"id": 1,
"status": 2,
"...": "...."
}
]
}
The best way to let the user know which filters can be added to an endpoint is to create an API Documentation with Swagger or Redoc.
To let the clients know what filters they can add for an endpoint, you send them the documentation where they can see exactly how it can be used.
Do you have another particular use case for which you want to send them on the response?
If you must necessarily send the filters in the body, you can create a FilterMixin and put it on all the ViewSets where you want to have this functionality and overwrite the list function. An example would be somthing like this:
class FiltersMixin:
def list(self, request, *args, **kwargs):
data = super().list(request, *args, **kwargs)
remaining_filters = list(set(self.filter_fields) - set(self.request.query_params.keys())
return {"filters": remaining_filters, **data}
This works if the filters on the viewset are added on the ViewSet using the filter_fields property.

Facebook Marketing API Campaign spend_cap field returns inconsistently

I'm trying to pull data for each of my Ad Campaigns from the Facebook Marketing API.
In the Ads Insights API there is only a 'spend' field that returns how much of the budget for that campaign has been spent so within the specified date range parameter. This is documented at
developers.facebook.com/docs/marketing-api/insights/fields/v2.9
I would like to get the 'spend_cap' field that's specified in the Reference section of the Marketing API located in the link below. One thing I noted was that there are no parameters available to this node, that may be why the spend_cap is not returning. This is documented at
developers.facebook.com/docs/marketing-api/reference/ad-campaign-group
I am using the following url to request the data.
https://graph.facebook.com/v2.9/{act_id}/campaigns?fields=name,spend_cap&access_token={access_token}
However, it returns the spend_cap field inconsistently, as shown below. I've only included a couple examples but I'm certain that all my campaigns are set up with spending caps.
data:[
{
"id": "##############",
"name": "name1",
"start_time": "2016-06-24T14:47:34-0400",
"stop_time": "2016-07-03T14:47:34-0400"
},
{
"id": "##############",
"name": "name2",
"spend_cap": "30000",
"start_time": "2016-05-16T11:57:10-0400"
},
{
"id": "##############",
"name": "name3",
"spend_cap": "15000",
"start_time": "2016-05-16T11:44:06-0400",
"stop_time": "2017-04-01T00:00:00-0400"
},
{
"id": "##############",
"name": "name4",
"start_time": "2016-05-13T15:34:41-0400",
"stop_time": "2017-05-13T09:46:44-0400"
}
]
The spend_cap at the campaign level is an optional field which is why it is only returned for some of the campaigns.
In general within the Graph API, if a field contains no data, this field will be omitted from the response.
Our SDKs abstract this for you so you can always access a field of an object, regardless of whether it was in the response, so if you're not using one of our SDKs, you'll have to do the same.

Should links in json-api responses be encoded?

Should the query params in links appearing in JSON-API responses be percent-encoded?
The examples from jsonapi.org are not encoded, as in:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
]
}
However, there is also a note regarding encoding parameters in requests:
GET /articles?include=author&fields[articles]=title,body&fields[people]=name HTTP/1.1
Accept: application/vnd.api+json
Note: The above example URI shows unencoded [ and ] characters simply for readability. In practice, these characters must be percent-encoded, per the requirements in RFC 3986.
Does this note apply only to requests? Or should responses also be percent-encoded, as in:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page%5Boffset%5D=2",
"last": "http://example.com/articles?page%5Boffset%5D=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
]
}
Yes, the note about encoding URI's in your question applies only to the request, not the response
With a json string returned inside a response, the only thing required to escape is the double quote character "
GET requests (not responses) are a different ball of wax. Anything in a GET request passed as a parameter in the URL must be URL encoded. So if you have a parameter url=http://some.url.com, the url on the right of the parameter assignment needs encoding.
It's tricky with POST and PUT requests. Depending on the content type set in the header you may need to encode. If your content type is application/json, you should not need to url encode anyting in your json string, (excluding the before mentioned ").
Now, if the specificed content encoding you state doesn't match what your sending (or if you didn't explicitly add one and it defaults to something), like if you send content-type: application/x-www-form-urlencoded but send a json string, the API service may or may not accept it and who know what how it will treat the content inside as far a url decoding it.

Update an entire resource collection in a REST way

I have a REST URI for a list of resources, something like:
http://foo.com/group/users
Each of these users has a sequence number and I want to expose a way to renumber those values for all the users in the collection and make this change available to everyone who accesses the list. Since this is an action on the collection as a whole, I'm not sure how to accomplish this.
I can envision a URL like http://foo.com/group/users?sequence=normalize but neither a PUT nor a POST really makes sense for the whole list, unless I submit the whole collection with the new numbers as the message data.
How can I make an update to an entire collection like this in a RESTful way without having to resend all the updated resources in the collection?
After the raffian's comment on my initial response, I reworked my answer to be more RESTful...
Use the method PATCH
This method is typically designed to update partially the state of a resource. In the case of a list resource, we could send a list with only the elements to update and the identifiers of elements in the list. The following request would be:
PATCH /group/users
[
{ "id": "userId1", "sequence": "newSequenceNumber1" },
{ "id": "userId2", "sequence": "newSequenceNumber2" },
(...)
]
Use the method POST on the list resource
This method is commonly used to add an element in the list managed by the resource. So if you want to leverage it for this action, we need to pass within the request an hint regarding the action to execute. We have the choice to add this either in a dedicated header or within the payload.
With the header approach, you will have something like that:
POST /group/users
X-Action: renumbering
[
{ "id": "userId1", "sequence": "newSequenceNumber1" },
{ "id": "userId2", "sequence": "newSequenceNumber2" },
(...)
]
With the payload approach, you will have something like that:
POST /group/users
{
"action": "renumbering",
"list": {
[
{ "id": "userId1", "sequence": "newSequenceNumber1" },
{ "id": "userId2", "sequence": "newSequenceNumber2" },
(...)
]
}
}
Hope it helps you,
Thierry
Semantically speaking, the HTTP PATCH method is the right way to go. This is also described in the currently chosen answer.
PATCH /group/users
[
{ "id": "userId1", "sequence": "newSequenceNumber1" },
{ "id": "userId2", "sequence": "newSequenceNumber2" },
...
]
However, the second method described in the chosen answer is not restful, because you invented new verbs inside a POST request. This is SOAP, not REST.
You can use both PATCH and POST on the URIs. I'd use PATCH if I were you. It's the best solution for bulk updates.

what is the REST url endpoint standard for download

I know it is HTTP GET rest type to get all or a particular resource details in REST webservices. What if i need to create a rest webservice which downloads a list of employee (filtered by search criteria) details into a file? It must be a GET call but how does the endpoint URL will look like?
//baseurl/employee/download?q=searchParam
Is this correct way to having my endpoint URL?
There is no specific 'download' concept in REST. Normally you have a collection resource
GET /baseurl/employee
that returns a lis of employyes:
[
{
"id": 123,
"firstName": "John",
"lastName": "Doe"
},
{
"id": 456,
"firstName": "Jahn",
"lastName": "Spencer"
}
]
You can filter this list using a query parameter:
GET /baseurl/employee?firstName=John
[
{
"id": 123,
"firstName": "John",
"lastName": "Doe"
}
]
That's it. The client can do what he wants with this response.
If the server supports multiple representations like JSON and XML, the client can request the representation he wants.