将cURL代码转换为c#izooto api

时间:2017-12-27 13:00:05

标签: c# api curl

我正在尝试访问推送通知的api。

这是cURL代码:

VERSION BUILD=9030808 RECORDER=FX
TAB T=1
'TAB CLOSEALLOTHERS
SET !DATASOURCE ppl.csv
'SET !LOOP 1
'SET !DATASOURCE_LINE {{!LOOP}}
FRAME F=3
TAG POS=1 TYPE=TEXTAREA FORM=ID:addmember ATTR=ID:add-members-textarea CONTENT={{!COL1}}<SP>{{!COL2}}<SP>{{!COL3}}<SP>{{!COL4}}<SP>{{!COL5}}<SP>{{!COL6}}<SP>{{!COL7}}<SP>{{!COL8}}<SP>{{!COL9}}<SP>{{!COL10}}<SP>{{!COL11}}<SP>{{!COL12}}<SP>{{!COL13}}<SP>{{!COL14}}<SP>{{!COL15}}<SP>{{!COL16}}<SP>{{!COL17}}<SP>{{!COL18}}<SP>{{!COL19}}<SP>{{!COL20}}<SP>{{!COL21}}<SP>{{!COL22}}<SP>{{!COL23}}<SP>{{!COL24}}<SP>{{!COL25}}
'TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:addmember ATTR=ID:add-members-add-button

我试图通过c#访问api。

C#代码:

curl -X POST \
  -H "Authentication-Token: {API_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "title" : "{NOTIFICATION_TITLE}",
    "message" : "{NOTIFICATION_MESSAGE}",
    "icon_url" : "{ICON_URL}",
    "banner_url" : "{BANNER_URL}",
    "landing_url" : "{LANDING_URL}",
    "actions" : [
       {
         "text" : "{BUTTON1_TEXT}",
         "url" : "{BUTTON1_URL}"
       },
       {
         "text" : "{BUTTON2_TEXT}",
         "url" : "{BUTTON2_URL}"
       }],
    "utm_source" : "{UTM_SOURCE}",
    "utm_medium" : "{UTM_MEDIUM}",
    "utm_campaign" : "{UTM_CAMPAIGN}",
    "ttl" : {TTL_SECONDS},
    "target" : {
            "type" : "all"
    }
  }' "https://apis.izooto.com/v1/notifications"

到达public string get() { try { WebRequest tRequest; tRequest = WebRequest.Create("https://apis.izooto.com/v1/notifications"); tRequest.Method = "post"; tRequest.ContentType = "multipart/form-data"; tRequest.Headers.Add("Authentication-Token", "xxxxxxxxx-yyyyyyyyy"); string imgurl = "https://cdnimg.izooto.com/9338/9883/93381513921358.png"; string landing_url = "http://www.maalaimalar.com/News/TopNews/2017/12/27110835/1136906/MK-Stalin-Slams-his-Brother-MK-Stalin-for-RK-Nagar.vpf"; string postData = "title=test&message=testmsg&icon_url=" + imgurl + "&landing_url=" + landing_url + ""; Console.WriteLine(postData); Byte[] byteArray = Encoding.UTF8.GetBytes(postData); tRequest.ContentLength = byteArray.Length; Stream dataStream = tRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream(); StreamReader tReader = new StreamReader(dataStream); String sResponseFromServer = tReader.ReadToEnd(); //lblStat.Text = sResponseFromServer; tReader.Close(); dataStream.Close(); tResponse.Close(); return sResponseFromServer; } catch (Exception e) { return e.Message; } } 时,我收到以下错误

错误:WebResponse tResponse = tRequest.GetResponse();

回复:System.Net.WebException: The remote server returned an error: (400) Bad Request.

我为卷曲代码引用了以下文档 https://docs.izooto.com/docs/push-to-all 任何人都可以为此提供解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:2)

在postData.Like中传递目标,

string postData= "{\n    \"title\" : \""+ title + "\",\n    \"message\" : \""+ message + "\",\n    \"icon_url\" : \""+ icon_url + "\",\n    \"banner_url\" : \"" + bannerUrl + "\",\n    \"landing_url\" : \"" + landing_url + "\",\n    \"target\" : {\n            \"type\" : \"all\"\n    }\n  }";