亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Twilio SMS api 不支持返回內容類型

Twilio SMS api 不支持返回內容類型

蕪湖不蕪 2023-05-17 15:58:49
目前我正在處理一個需要將 SMS 檢索功能添加到系統中的項目。我已經使用 spingboot 來構建應用程序。所有的實現都完成了,我已經按照 twillio 上的所有必要配置從客戶端檢索短信。當我向 Twilio api 發送短信時,它會Unsupported Media Type在調試器中說明。我還向 api 發送了所需的內容類型。當我向 twilio 提供的號碼發送短信時會發生這種情況。但是郵遞員調用應用程序工作正常。package com.crustykrabs.application.service;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import static spark.Spark.*;import com.twilio.twiml.MessagingResponse;import com.twilio.twiml.messaging.Body;import com.twilio.twiml.messaging.Message;@RestControllerpublic class TextMessageController {    @PostMapping(path = "/messages/textmessages/receive", consumes = "application/xml", produces = "application/xml")    public @ResponseBody ResponseEntity<String> receive() {            Body body = new Body                    .Builder("The Robots are coming! Head for the hills!")                    .build();            Message sms = new Message                    .Builder()                    .body(body)                    .build();            MessagingResponse twiml = new MessagingResponse                    .Builder()                    .message(sms)                    .build();            return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(twiml.toXml());    }}
查看完整描述

2 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

恕我直言,api 調用是最佳選擇。RestTemplate請使用如下所示使用已發布的 API 進行實施。


public void sendOTP() {

    RestTemplate restTemplate = new RestTemplate();


    String message = "Your PIN for account verification is 123456";

    String user = "******405e4c****19d0******";

    String password = "******";

    String smsurl = "https://api.twilio.com/2010-04-01/Accounts/"+user+"/Messages.json";


    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);


    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();

    map.add("From", "+1334384****");

    map.add("To", "+999999999");

    map.add("Body", message);


    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, headers);


    try {

        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));

        Object response = restTemplate.postForObject(smsurl, httpEntity, Object.class);

        LOG.info("Sms Response: {}", gson.toJson(response));

    } catch(Exception e) {

        LOG.error(e.getMessage());

    }

}


查看完整回答
反對 回復 2023-05-17
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

您認為客戶端沒有指定內容類型。請補充content-type: application/xml。


如果你有 spring boot,你可以通過添加以下依賴項來修復它:


<dependency>

     <groupId>com.fasterxml.jackson.dataformat</groupId>

     <artifactId>jackson-dataformat-xml</artifactId>

     <version>2.9.8</version>

</dependency>


查看完整回答
反對 回復 2023-05-17
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號