What is the most appropriate HTTP method to send info? - express

I would like to know what is the most appropriate HTTP method to send info about an receive some logical result in a response.
I have to build an endpoint to receive some information about address and postal code, and this endpoint should process this data to respond information about the result of the process.
The info that I have to send is an address, then it could contain special characters, or whitespaces, and the postal code.
I was thinking how to do it, I thought in two different ways, but I would like to know from you what will be the best approach.
First:
GET
www.myapi.com/georefence?postal_code=33210&address=street 56 1254
Send the information in the path as params
POST
www.myapi.com/georefence
Send the information in the body as object
body: { "postal_code": 33210, "address": "street 56 1254" }
Expected:
Information about the longitude, latitude, if the postal code was found successfully, the precision, and other things.
Thanks.

Related

ASP.NET Core Web API - Return a string message with a NoContent Response?

I have some records in the database that may or may not have some information for a certain column, and that's OK.
If a consumer of the endpoint runs into one of these records, I'd like to return "NoContent", because, well, there's No Content for that record that we can execute on. BUT ... I'd like to give them a message why, as there are two distinct reasons.
When looking at other status codes, this works:
return Accepted("I SEE THIS STRING!");
It actually shows up in the Response Header as the field location (I think, don't make me run this project again!)
This works for say Internal Server error:
return StatusCode(500, "I SEE THIS TOO!");
But, for NoContent there is no constructor like Accepted, where you can pass an object. Also, this shows me no string whatsoever:
return StatusCode(204, "WHY CAN'T I SEE THIS STRING?!");
Help!
204 (No Content) returns an HttpResponseMessage with, well, No Content.
If you do have content to return that the user can consume (the two reasons), then you don't have a No Content scenario.
Perhaps an Ok (200) with a custom ContentNotProvided class that holds a message, which can be consumed by the client, will do the trick?
Alternatively, return a BadRequest (400) with a message.

Http status code when data not found in Database

I'm trying to understand which Http Status Code to use in the following use case
The user tries to do a GET on an endpoint with an input ID.
The requested data is not available in the database.
Should the service send back:
404 - Not Found
As the data is NOT FOUND in the database
400 - Bad Request
As the data in the input request is not valid or present in the db
200 - OK with null response
200 - OK with an error message
In this case we can use a standard error message, with a contract that spans across all the 200 OK responses (like below).
BaseResponse {
Errors [{
Message: "Data Not Found"
}],
Response: null
}
Which is the right (or standard) approach to follow?
Thanks in advance.
Which is the right (or standard) approach to follow?
If you are following the REST API Architecture, you should follow these guidelines:
400 The request could not be understood by the server due to incorrect syntax. The client SHOULD NOT repeat the request without modifications.
It means that you received a bad request data, like an ID in alphanumeric format when you want only numeric IDs. Typically it refers to bad input formats or security checks (like an input array with a maxLength)
404 The server can not find the requested resource.
The ID format is valid and you can't find the resource in the data source.
If you don't follow any standard architecture, you should define how you want to manage these cases and share your thought with the team and customers.
In many legacy applications, an HTTP status 200 with errors field is very common since very-old clients were not so good to manage errors.

What does Mandrill status = "invalid" mean in Send Message API response

Mandrill official documentation says that the status property in the response of Send Message API request is:
the sending status of the recipient - either "sent", "queued", "scheduled", "rejected", or "invalid"
Can't find anywhere what does invalid status indicate.
Any idea, referece ?
One reason I have found is an invalid email address. For example having two dots in a domain like this: alice#example..com
I had this happen because some of the properties in the JSON I posted had uppercase letters when the mandrill api was expecting lowercase.
I was using newtonsoft and solved it by adding JsonProperty attributes to my objects as described here: https://stackoverflow.com/a/34071205

Autosend email invitation via APIs in SurveyMonkey

I have a feedback survey form need to send out daily when i received a new list of recipients.
Is it possible we can schedule a particular survey email invitations to be send out daily/weekly at certain time (e.g. midnight 12AM) with the new contacts list (will be different, or same email address may repeat) each day. At the same time, i still want to keep track the old recipients responses/bounced messages each time/in total. Or what is the best approach?
I m planning to update the same email invitation recipients list(Collectors) thru API with a pre-create recipients list.
Here are some findings from SUrveyMonkey APIs doc site:
- contacts_write: to Create/Modify Contacts
- collectors_write: to Create/Modify Collectors
- /collectors/{id}/messages/{id}/recipients/bulk
- /collectors/{COLLECTOR_ID}/messages/{MESSAGE_ID}/send
Any better approaches can share with me?
Thanks
Yes the two endpoints you specified would be the way to go. There's a number of ways to do this. One way is to have a script run on a cron job.
Something like 00 00 * * * ./your_script.
Then make sure your script pulls the recipients you want to send to from whatever data source you have for that and then call SurveyMonkey's API to:
1) Create a new message on the collector
POST /v3/collectors/<collector_id>/messages
{
"type": "invite"
}
2) Insert all the recipients you pulled into the message
POST /v3/collectors/<collector_id>/messages/<message_id>/recipients/bulk
{
"contacts": [{
"email": "test#example.com",
"first_name": "Test",
"last_name": "Example"
}...]
}
3) Then send out the message immediately
POST /v3/collectors/<collector_id>/messages/<message_id>/send
{}
That's one way to have a scheduled task run, using the SurveyMonkey API.

Is it possible to send a single message to multiple numbers at a time using Twilio?

I'm developing an app that allows users to add people, info, and Name/phone, or select multiple numbers from their iPhone contact list to send SMS messages to the selected numbers. the problem is Twillio API needs to be call every time per number. Is their any way to call the API once for multiple numbers?
Is it possible to send message to multiple number at a time?
Is it possible to send multiple messages?
Thanks in advance
It's not possible, you need to iterate through the list and make one request per message (which is probably better than batching it and dealing with the potential of multiple errors / resends).
Each new SMS message from Twilio must be sent with a separate REST API request. To initiate messages to a list of recipients, you must make a request for each number to which you would like to send a message. The best way to do this is to build an array of the recipients and iterate through each phone number.
const numbersToMessage = ["+15558675310", "+14158141829", "+15017122661"]
numbersToMessage.forEach(async number => {
const message = await client.messages.create({
body: 'message body',
from: '+16468635472',
to: number
});
console.log(message.status)
});
Yes this is possible. Infact i'm trying to do the same thing at the moment(which is why i'm here) and Twilio has some advanced stuff that lets us achieve this.
Assuming you have a twilio ssid, twilio auth token and a twilio phone number, the next thing you have to do is create a "Twilio Messaging Service" from the dashboard. You can use the ssid of the created messaging service and use or if you want to send a message to like 10k numbers in one go, you create a "Twilio Notify Service" from the dashboard which takes the previously created messaging service as part of its configuration. Once this is done you can call the twilio.notifications.create() and pass bindings({ binding_type: 'sms', address: number }) for each phone number to it.
Complete explanation found in this twilio blog right here with perfectly working code.
https://www.twilio.com/blog/2017/12/send-bulk-sms-twilio-node-js.html
Yes it is possible to send message to multiple user's from your Twilio Number.
You can try this for your node.js file:
var arr = ["+1xxxxxxxxxx","+1xxxxxxxxx"];
arr.forEach(function(value){console.log(value);
client.messages.create({
to:value,
from: "+19253504188",
body: msg,
}, function(err,message){
console.log(err);
});
});
Yes it is possible. You have to provide the numbers as a list and iterate API call.
For example send a message to two numbers.
numbers = ['+1234562525','+1552645232']
for number in numbers:
proxy_client = TwilioHttpClient()
proxy_client.session.proxies = {'https': os.environ['https_proxy']}
client = Client(account_sid, auth_token, http_client=proxy_client)
message = client.messages \
.create(
body="Your message",
from_='Your Twilio number',
to=number
)