Mailgun POST /messages API always throw 401 forbidden

女生的网名这么多〃 提交于 2019-12-11 04:07:25

问题


I am trying to send Mailgun POST /messages API request in Android using retrofit library.

Following is retrofit request:

HTTP POST https://api:key-xxx/v3/sandboxxxx/messages

Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 148

from=Excited+User+%3Cmailgun%40sandboxxxxmailgun.org%3E&to=vir.jain%40gmail.com&subject=Hello&text=Testing+awesomeness

Response: HTTP 401 https://api:key-xxx/v3/sandboxxxx.mailgun.org/messages (1966ms)
Server: nginx/1.7.9
Date: Mon, 15 Jun 2015 10:00:37 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 9
Connection: keep-alive
WWW-Authenticate: Basic realm="MG API"
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1434362440805
OkHttp-Received-Millis: 1434362441086
Forbidden

When I try this same request on Postman, it works perfectly & email is sent properly. But when sending using retrofit library from Android fails with Forbidden.

My Retrofit API: @FormUrlEncoded @POST("/messages") public void sendEmail(@Field("from") String from, @Field("to") String to, @Field("subject") String subject, @Field("text") String text, RestCallback objectRestCallback);

Base URL is: https://api:key-xxx/v3/sandboxxxsandboxxxx.mailgun.org

Everything is form URL encoded. But I'm really not sure what going wrong. Can anyone please help me out whats wrong with above API?

Thank you, Mahavir


回答1:


I added following authorization header with Base64 encoded string of API key to the request, it started working.

request.addHeader("Authorization", "Basic "+getBase64String("api:key-xxxYOUR-API-KEY>"));

@Selvin: Thanks so much for sending wiki link :).



来源:https://stackoverflow.com/questions/30842841/mailgun-post-messages-api-always-throw-401-forbidden

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!