Forum Discussion
kbr
2 months agoNew Contributor
Here a workaround. AI suggest me to do my POST request with a groovy script (find bellow the groovy script that I used)
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.client.methods.HttpPost
import org.apache.http.util.EntityUtils
CloseableHttpClient httpClient = HttpClients.createDefault()
// Définir l'URL de votre service
def url = context.expand('${#Global#endpoint.api-interco-url}')+'/interco/lot-bs'
HttpPost post = new HttpPost(url)
// Construire le corps de la requête pour multipart/form-data
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
// Ajouter des champs de formulaire
// log.info context.expand('${=testCase.name}')
builder.addTextBody("filename", context.expand('${=testCase.name}') )
// log.info context.expand('${#TestCase#encodedFile}')
builder.addTextBody("content", context.expand('${#TestCase#encodedFile}') )
// Construire l'entité multipart
def multipartEntity = builder.build()
post.setEntity(multipartEntity)
// Créer les en-têtes de la requête
// post.setHeader("Content-Type", "multipart/form-data") // laisser le système déterminer le Content-Type
post.setHeader('Authorization', 'Bearer ' + context.expand('${#TestCase#intercoApiToken}'))
post.setHeader('X-Gravitee-Api-Key', context.expand('${#Project#x-gravitee-api-key.header.attribute}'))
post.setHeader('Content-Disposition', 'form-data')
post.setHeader('Content-transfert-encoding', '7bit')
// post.setHeader('Connection','Keep-Alive')
// Envoyer la requête HTTP POST
def response = httpClient.execute(post)
// Gérer la réponse
def responseContent = EntityUtils.toString(response.getEntity())
if (response.statusLine.statusCode == 200) {
log.info "Réponse réussie : ${responseContent}"
} else {
log.error "http status error code : ${response.statusLine.statusCode}"
log.error "body content : ${responseContent}"
}
// Fermer le client
httpClient.close()
Related Content
- 6 years ago
Recent Discussions
- 15 hours ago
- 17 hours ago