简体   繁体   中英

posting json to curl from asp.net core 2.0 HttpClient with additional information

I am trying to call below url from my code using asp.net core 2.0 web api:

curl -H "Content-Type: text/plain" --data '{"key":"[your-key-here]","addr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb","callback":" https://mystore.com?invoice_id=123 ","onNotification":"KEEP", "op":"RECEIVE", "confs": 5}' https://api.blockchain.info/v2/receive/balance_update

I created a Model to post with cURL like this:

public class ReceiveModel
    {
        public string key { get; set; }
        public int id { get; set; }
        public string addr { get; set; }
        public string op { get; set; }
        public int confs { get; set; }
        public string callback { get; set; }
        public string onNotification { get; set; }
    }

and calling it like this:

public const string baseurl = "https://api.blockchain.info/v2/receive/balance_update";
public async Task<object>ReceiveBitcoins(ReceiveModel recd)
        {
            recd.key = "xxxxxxxx";

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(rooturl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));

            var response = await client.PostAsync(rooturl, recd);
   return response;
        }

Please note recd is the ReceivedModel whose other fields are coming from WepApi post and api key is getting entered here only.

Is this the correct implementation? Do I need to take care of -H and --data, if so, how may I achieve this.

The response I get from this is :

    {"result":{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Length","value":["109"]},{"key":"Content-Type",
"value":["application/json"]}]},"statusCode":400,"reasonPhrase":"Bad Request","headers":[{"key":"Connection","value":["keep-alive"]},{"key":"Date",
"value":["Fri, 26 Jan 2018 11:31:19 GMT"]},{"key":"Via","value":["1.1 google"]},{"key":"Server","value":["cloudflare"]},{"key":"Set-Cookie","value":["__cfduid=dedf2c6d34e69eeeeeeeeec9b7f1516966278; expires=Sat, 26-Jan-19 11:31:18 GMT; 
path=/; domain=.blockchain.info; HttpOnly"]},{"key":"X-Blockchain-CP-F","value":["2fgz 0.010"]},{"key":"X-Blockchain-Server","value":["BlockchainFE/1.0"]},
{"key":"Strict-Transport-Security","value":["max-age=31536000; includeSubDomains; preload"]},{"key":"X-Content-Type-Options","value":["nosniff"]},{"key":"X-XSS-Protection","value":["1; mode=block"]},{"key":"Alt-Svc","value":["clear"]},
{"key":"Expect-CT","value":["max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""]},{"key":"CF-RAY","value":["3e33252b3fdd8a55-BOM"]}],"requestMessage":{"version":{"major":1,"minor":1,"build":-1,
"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Type","value":["application/json"]},{"key":"Content-Length",
    "value":["222"]}]},"method":{"method":"POST"},"requestUri":
    "https://api.blockchain.info/v2/receive/balance_update","headers":[{"key":"Accept","value":["text/plain"]},
    {"key":"x-ms-request-root-id","value":["1e0xxxxx669-46xxx11d79afac17e"]},{"key":"x-ms-request-id","value":["|1e0xxx69-462511xxxxac17e.1."]},{"key":"Request-Id","value":["|1e0axxx9-462xxxx1xxxxe.1.1."]}],"properties":{}},"isSuccessStatusCode":false},"id":4,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}

This is

Please help.

When you fill out this request through CURL, does it work? Is your "ReceiveBitcoins" method invoked?

curl -H "Content-Type: text/plain" --data '{"addr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb","callback":" https://mystore.com?invoice_id=123 ","onNotification":"KEEP", "op":"RECEIVE", "confs": 5}'

Yes, in fact, it will return the result for any callback URL that you enter ... Do you have the documentation for this endpoint " https://api.blockchain.info/v2/receive/balance_update "? In the documentation of this API, it tells you how it sends the data to your url callback.

Yes, in fact, it will return the result to any callback URL that you enter.

In order for you to receive this information, you must create a new method to receive this response, for example:

//This method is invoked via the url "https://mystore.com/balance-update".
public async Task<object>BalanceUpdate()
{
     // Perform the data processing here. According to the API documentation.
}

And in your callback, you specify the url to access this method " https://mystore.com/balance-update ", meaning your callback would look like this "callback = https: //mystore.com/balance-update" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM