gRPC JSON transcoding : using repeated fields in query parameters - json

I'm trying to make a request via Swagger using JSON transcoding, but I'm encountering an error when I use a repeated field parameter in my request. The error message I'm receiving is:
response
I've attached my code snippet below for reference. Can someone help me figure out what's causing this issue and provide a solution for making a request with a repeated field parameter using JSON transcoding in Swagger?
Here's my code snippet:
syntax = "proto3";
import "google/api/annotations.proto";
package greet;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
get: "/v1/greeter/{name}/{ids}"
};
}
}
message HelloRequest {
string name = 1;
IdsRequest ids = 2;
}
message HelloReply {
string message = 1;
}
message IdsRequest {
string id = 1;
}
My request:
request
Note: I am using a very simple example just to shorten the code for this question.

Related

ReactJS doesn't recognize a value in an object in an array

I am making a web app with express + react and I'm sending JSON data.
I am fetching this data with axios and using setState to set the messages variable to response.data.messages
{
"messages":{
"message":{
"username":"Khigoris"
}
}
}
and it doesn't let me do this
<p>{messages.message.username}</p>
But it says it is undefined
I am new to using JSON so I think it's the syntax but I need help.
It looks like the messages object is wrapped in another object, i.e your code looks like:
const value = {
"messages":{
"message":{
"username":"Khigoris"
}
}
}
So you should use:
<p>{value.messages.message.username}</p>

API restSharp JSON payload remove Parameter

I am trying to create a 400 bad request, by removing an existing parameter from the payload in my step definitions. So, far i have tried various scenarios but not been successful in removing a parameter from the payload object. The payload object has been created a in separate class, at run time i just want to remove a single parameter (paymentAmount). How do i do this?
[Given(#"the person has not sent the payment amount")]
public void GivenTheValuerHasNotSentThePaymentAmount()
{
var requestHandler = scenarioContext.Get<RestRequestHandler>("requestHandler");
var invalidPaymentRequest = StatusRequest.PaymentStatus();
requestHandler.APIEndPoint = string.Format(requestHandler.APIEndPoint, paymentnumber);
requestHandler.Body = JsonConvert.SerializeObject(invalidPaymentRequest);
var response = requestHandler.MakeAPICall();
scenarioContext.Add("response", response);
}

ktor receive json body twice

I cannot receive the json body of the ktor HttpClient twice.
For the server there is a DoubleReceive feature but I don't see how I can use this when doing client calls.
I want to call a different microservice which either returns some json or when there is an error it return e.g. status 500 and a error description a json payload.
so I tried to HttpResponseValidator and in this only allows a readBytes with this code
HttpResponseValidator {
validateResponse { response ->
val statusCode = response.status.value
val originCall = response.call
if (statusCode < 300 || originCall.attributes.contains(ValidateMark)) return#validateResponse
response.use {
val exceptionCall = originCall.save().apply {
attributes.put(ValidateMark, Unit)
}
//try parse error from json payload which other microservice usually send
exceptionCall.response.receiveError()?.also { throw mapErrors(response, it) }
//default ktor exception mapping
when (statusCode) {
in 300..399 -> throw RedirectResponseException(response)
in 400..499 -> throw ClientRequestException(response)
in 500..599 -> throw ServerResponseException(response)
}
if (statusCode >= 600) {
throw ResponseException(response)
}
}
}
}
receiveError can be used as JacksonConfig.defaultMapper.readValue<ServiceErrorResponse>(this.readBytes()) but will throw a DoubleReceivException if you just call response.receive<ServiceErrorResponse>()
The reason for this is that the receive function first checks a received atomicBoolean.
TL;DR
Now I wonder if there are any ideas on how you handle error payloads or do you just not use them? I am new to microservice in such a manner and it was a requirement to add them. Ktor is a new addition. How do you communicate error infromation between services?
Also is there a way to use the DoubleReceive feature in the client. Because HttpClient(){install(DoubleReceive)} does not work as it is not an ApplicationFeature and not a ClientFeature.
Ktor has developed an experimental plugin called Double Receive, you can use it to receive request body as much as you want.
Read more: https://ktor.io/docs/double-receive.html

SoapUI REST + JSON Mock Service: How can I return data from the request in a response?

I have a SoapUI REST (i.e. non-SOAP) mock service that returns a response for a POST request.
The request and response both contain JSON content.
At the moment, I can get it to return a static response and that works fine, but I want some of the values in the response to be dynamically sourced from the request.
So if I have this request:
{
"the_request":{
"abc":"123",
}
How can I get the value of "abc" copied in the response?
Investigation has lead me to believe I can do this via including a variable in the response, something like:
Response:
{
"the_response":{
"value_from_request":"${#MockResponse#Request#the_request#abc}",
"other":"stuff",
}
And then implementing a script to populate the variable in the response, via the Script tab.
How can I then populate this with data from the request?
Currently SoapUI just generates an empty value
"value_from_request":"",
Tried using mockRequest.requestContent in the Script tab, but have not found how to obtain the "123" value from it.
OK, worked this out. So the response message can simply reference a variable in the requestContext like so:
Response:
{
"the_response":{
"value_from_request":"${the_value}",
"other":"stuff",
}
And a groovy script can be used to parse the JSON request content and populate "the_value" or whatever you like in the requestContext:
// Parse the JSON request.
def requestBody = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())
// Set up "the_value" from the request message.
requestContext.the_value = requestBody.the_request.abc
// Bit of logging so can see this in the "script log" tab.
log.info "Value extracted from request: ${requestContext.the_value}"
I think the script should be like this
def requestBody = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())
context.setProperty("the_value",requestBody.the_request.abc)

How do I convert json into a kotlin object in ktor using content negotiation?

I'm new to Ktor, and I'm trying to build a backend that processes login credentials. I'm attempting to use the content negotiation feature to convert JSON into a native Kotlin object, but I keep getting an unsupported media type exception. Here is my code:
fun Application.main() {
install(CallLogging)
install(DefaultHeaders)
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
routing {
get("/") {
call.respondHtml {
head {
title("Kotlin Webapp")
script {
type = ScriptType.textJScript
src = "main.bundle.js"
}
}
body {
div {
id = "root"
}
}
}
}
post("/login") {
val credentials = call.receive<Credentials>()
println(credentials)
}
}
}
data class Credentials(val username: String, val password: String)
And here is the incoming Json I am trying to convert, which I am sending via XMLHttpRequest:
{"username":"Jamdan2","password":"sometext"}
I have searched the web for answers, but could not find what I am doing wrong. Can anybody help?
For completeness, since my comment seems to have helped:
You need to be sure that the request's Content-Type header is set to the correct value, in this case, application/json, otherwise the server cannot be entirely sure what to do with the received content.
In requests, (such as POST or PUT), the client tells the server what type of data is actually sent.
xhr.setRequestHeader("Content-Type", "application/json");