Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CPPREST] Fixed multipart files upload implementation #6518

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
throw ApiException(415, U("{{classname}}->{{operationId}} does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped this line because it doesn't make any good:

  1. It prevents the request.set_body from setting a custom Content-Type (it simply ignores the later contentType passed to request.set_body, and thus, the multipart boundary marker cannot be set).
  2. request.set_body sets the Content-Type in the ApiClient anyway.

Copy link
Contributor Author

@frol frol Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just seen issue #4370, so I will test it more thoroughly and report my findings (I still don't believe this line is necessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my tests with POST endpoints work fine on my end:

  • URL-encoded forms
  • Multipart forms
  • JSON body forms (NOTE: it is not enough to just set the Content-Type to make the form JSON-based)

Thus, I conclude that this change is fine.


{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ pplx::task<web::http::http_response> ApiClient::callApi(
uploadData.add(ModelBase::toHttpContent(kvp.first, kvp.second));
}
std::stringstream data;
postBody->writeTo(data);
uploadData.writeTo(data);
auto bodyString = data.str();
auto length = bodyString.size();
request.set_body(concurrency::streams::bytestream::open_istream(std::move(bodyString)), length, contentType);
request.set_body(concurrency::streams::bytestream::open_istream(std::move(bodyString)), length, U("multipart/form-data; boundary=") + uploadData.getBoundary());
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/cpprest/ApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ pplx::task<web::http::http_response> ApiClient::callApi(
uploadData.add(ModelBase::toHttpContent(kvp.first, kvp.second));
}
std::stringstream data;
postBody->writeTo(data);
uploadData.writeTo(data);
auto bodyString = data.str();
auto length = bodyString.size();
request.set_body(concurrency::streams::bytestream::open_istream(std::move(bodyString)), length, contentType);
request.set_body(concurrency::streams::bytestream::open_istream(std::move(bodyString)), length, U("multipart/form-data; boundary=") + uploadData.getBoundary());
}
else
{
Expand Down
24 changes: 0 additions & 24 deletions samples/client/petstore/cpprest/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ pplx::task<void> PetApi::addPet(std::shared_ptr<Pet> body)
throw ApiException(415, U("PetApi->addPet does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -224,9 +221,6 @@ pplx::task<void> PetApi::deletePet(int64_t petId, utility::string_t apiKey)
throw ApiException(415, U("PetApi->deletePet does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -329,9 +323,6 @@ pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByStatus(std::vect
throw ApiException(415, U("PetApi->findPetsByStatus does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -459,9 +450,6 @@ pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByTags(std::vector
throw ApiException(415, U("PetApi->findPetsByTags does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -586,9 +574,6 @@ pplx::task<std::shared_ptr<Pet>> PetApi::getPetById(int64_t petId)
throw ApiException(415, U("PetApi->getPetById does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (api_key) required
{
utility::string_t apiKey = apiConfiguration->getApiKey(U("api_key"));
Expand Down Expand Up @@ -732,9 +717,6 @@ pplx::task<void> PetApi::updatePet(std::shared_ptr<Pet> body)
throw ApiException(415, U("PetApi->updatePet does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -843,9 +825,6 @@ pplx::task<void> PetApi::updatePetWithForm(int64_t petId, utility::string_t name
throw ApiException(415, U("PetApi->updatePetWithForm does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down Expand Up @@ -953,9 +932,6 @@ pplx::task<std::shared_ptr<ApiResponse>> PetApi::uploadFile(int64_t petId, utili
throw ApiException(415, U("PetApi->uploadFile does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (petstore_auth) required
// oauth2 authentication is added automatically as part of the http_client_config

Expand Down
12 changes: 0 additions & 12 deletions samples/client/petstore/cpprest/api/StoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ pplx::task<void> StoreApi::deleteOrder(utility::string_t orderId)
throw ApiException(415, U("StoreApi->deleteOrder does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("DELETE"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -196,9 +193,6 @@ pplx::task<std::map<utility::string_t, int32_t>> StoreApi::getInventory()
throw ApiException(415, U("StoreApi->getInventory does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;

// authentication (api_key) required
{
utility::string_t apiKey = apiConfiguration->getApiKey(U("api_key"));
Expand Down Expand Up @@ -327,9 +321,6 @@ pplx::task<std::shared_ptr<Order>> StoreApi::getOrderById(int64_t orderId)
throw ApiException(415, U("StoreApi->getOrderById does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("GET"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -463,9 +454,6 @@ pplx::task<std::shared_ptr<Order>> StoreApi::placeOrder(std::shared_ptr<Order> b
throw ApiException(415, U("StoreApi->placeOrder does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("POST"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down
24 changes: 0 additions & 24 deletions samples/client/petstore/cpprest/api/UserApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ pplx::task<void> UserApi::createUser(std::shared_ptr<User> body)
throw ApiException(415, U("UserApi->createUser does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("POST"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -241,9 +238,6 @@ pplx::task<void> UserApi::createUsersWithArrayInput(std::vector<std::shared_ptr<
throw ApiException(415, U("UserApi->createUsersWithArrayInput does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("POST"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -366,9 +360,6 @@ pplx::task<void> UserApi::createUsersWithListInput(std::vector<std::shared_ptr<U
throw ApiException(415, U("UserApi->createUsersWithListInput does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("POST"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -466,9 +457,6 @@ pplx::task<void> UserApi::deleteUser(utility::string_t username)
throw ApiException(415, U("UserApi->deleteUser does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("DELETE"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -566,9 +554,6 @@ pplx::task<std::shared_ptr<User>> UserApi::getUserByName(utility::string_t usern
throw ApiException(415, U("UserApi->getUserByName does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("GET"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -696,9 +681,6 @@ pplx::task<utility::string_t> UserApi::loginUser(utility::string_t username, uti
throw ApiException(415, U("UserApi->loginUser does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("GET"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -818,9 +800,6 @@ pplx::task<void> UserApi::logoutUser()
throw ApiException(415, U("UserApi->logoutUser does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("GET"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down Expand Up @@ -937,9 +916,6 @@ pplx::task<void> UserApi::updateUser(utility::string_t username, std::shared_ptr
throw ApiException(415, U("UserApi->updateUser does not consume any supported media type"));
}

//Set the request content type in the header.
headerParams[U("Content-Type")] = requestHttpContentType;


return m_ApiClient->callApi(path, U("PUT"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/cpprest/model/Pet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

#include "ModelBase.h"

#include "Tag.h"
#include <cpprest/details/basic_types.h>
#include "Category.h"
#include <cpprest/details/basic_types.h>
#include <vector>
#include "Tag.h"

namespace io {
namespace swagger {
Expand Down