Converting MailGun cUrl to Coldfusion

2019-08-08 17:00发布

问题:

I'm attempting to perform actions on a Mailgun mailing list using Coldfusion.

Mailgun have supplied the following cUrl:

curl -s --user 'api:key-xxxMyAPIkeyxxx' -G \
https://api.mailgun.net/v3/lists/myList@myDomain.mailgun.org/members

I can get a good response by simply entering

https://api:key-xxxmyAPIkeyxxx@api.mailgun.net/v3/lists/myList@myDomain.mailgun.org/members

But I've had no luck converting this into a CFHTTP call:

<cfhttp url="https://api:key-xxxmyAPIkey@api.mailgun.net/v3/lists/myList@myDomain.mailgun.org/members" method="GET" result="resultName">

<cfdump var="#resultName#">

This returns a whole load of errors:

object ErrorDetail [string]
Mimetype [string] application/json Statuscode [string] 401 Unauthorized Filecontent [object]
object [empty] Responseheader [object] object Connection [string] close Content-Length [number] 0 Date [string] Mon, 10 Aug 2015 16:46:22 GMT Explanation [string] Unauthorized Content-Type [string] application/json Http_Version [string] HTTP/1.1 Www-Authenticate [string] Basic realm="MG API" Server [string] nginx/1.7.9 Status_Code [number] 401 Text [boolean] false Charset [string]
Header [string] HTTP/1.1 401 Unauthorized Content-Type: application/json Connection: close Date: Mon, 10 Aug 2015 16:46:22 GMT Www-Authenticate: Basic realm="MG API" Content-Length: 0 Server: nginx/1.7.9

Any ideas ??

回答1:

The Mailgun API says basic authentication is used. Try supplying the credentials via the username and password attributes instead of the URL:

<cfhttp url="https://api.mailgun.net/v3/lists/myList@myDomain.mailgun.org/members" 
      method="GET" 
      username="api" 
      password="key-xxxYourAPIKeyxxx"
      result="resultName" />