Как указать тип контента в запросе в groovy? - PullRequest
2 голосов
/ 16 августа 2010

Я пытаюсь использовать отличный httpbuilder для публикации в веб-службе Microsoft Exchange (EWS).Моя проблема в том, что я не могу установить правильный тип содержимого запроса.Кажется, у библиотеки здесь свой разум.

У кого-нибудь есть идея?

Приветствия, Стефан

Вот мой код:

    url = "http://exchangeserver/ews/Exchange.asmx"
    p_body = "<soap request >...";
    p_contentType = "text/xml; charset=utf-8"
    customHeaders = ["SOAPAction":"LONG_URL"]

    def http = new HTTPBuilder(url);
    http.auth.basic(authMap.username, authMap.password)

    // contentType: p_contentType,
    http.request( POST ) 
    {
        contentType = ContentType.TEXT // We dont want to get the response parsed
        headers['Accept'] = "*/*"; // Just make sure we accept everything

        // Support additional headers
        for (x in customHeaders) {
            headers[x] = customHeaders[x]   
        }


        /// Exchange expects "text/xml; charset=utf-8" and nothing else :(

//  This sends text/plain
//      body = p_body
//      requestContentType = p_contentType

        // This sends application/xml, not my "text/xml; charset=utf-8" content-type.
            send p_contentType, p_body 

        // a successfull request should be "logged" ;)
        response.success = { resp, xml ->
            println xml
        }
    }

1 Ответ

1 голос
/ 16 августа 2010

Что ж, читая и отлаживая код, я обнаружил, что это мой текущий обходной путь / решение. Не так красиво, как я надеялся:

// We overwrite the default text/xml encoder,
// because it replaces our contentType with 'application/xml'
// But Exchange only likes 'text/xml; charset=utf-8'
http.encoder.'text/xml' = {
    body -> def se = new StringEntity(body, "utf-8")
    se.setContentType("text/xml; charset=utf-8")
    return se
}
...