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

Support plain-text responses #61

Open
jvmlet opened this issue Apr 21, 2020 · 1 comment
Open

Support plain-text responses #61

jvmlet opened this issue Apr 21, 2020 · 1 comment

Comments

@jvmlet
Copy link

jvmlet commented Apr 21, 2020

Hello
We have Spring MVC RestEndpoint that returns ResponseEntity<String> :

 @PutMapping(value = "/path", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> method(){
        return ResponseEntity.ok()
       .contentType(MediaType.TEXT_PLAIN)
       .body("Some String Value"));
    }

When invoking the http request , the reponseType option is not passed:

//from generated endpoint.ts 
request(data: RequestData): Observable<any> {
....
        return this.httpClient.request(data.method, url, { params: params, body: data.requestBody })
            .pipe(map(r => data.responseType ? this.config.deserialize(r, data.responseType) : r));
    }

and defaults to json

//  http.d.ts
request(method: string, url: string, options?: {
        body?: any;
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        responseType?: 'json';
        reportProgress?: boolean;
        withCredentials?: boolean;
    }): Observable<Object>;

which yields JSON parsing error :

//from  http.js
if (req.responseType === 'json' && typeof body === 'string') {
                    // Save the original body, before attempting XSSI prefix stripping.
                    var originalBody = body;
                    body = body.replace(XSSI_PREFIX, '');
                    try {
                        // Attempt the parse. If it fails, a parse error should be delivered to the user.
                        body = body !== '' ? JSON.parse(body) : null;
                    }
....
)

While the straight forward work-around is to return StringWrapper object as json media-type:

@Data
class StringWrapper {
private String string;
}

@PutMapping(value = "/path")
    public ResponseEntity<StringWrapper> method() {
        return ResponseEntity.ok()
       .body(new StringWrapper ("SomeString Value")));
    }

it would be great to support such case out of the box.
Thanks

@komu komu changed the title String response type is tried to be json-deserialized Support plain-text responses Apr 24, 2020
@komu
Copy link
Member

komu commented Apr 24, 2020

Right, this seems like a reasonable enhancement. I think supporting just text/plain is a good start with obvious implementation, but this could even be enhanced to support other types in the future (probably that would mean that you'd need to register a custom deserializer on the client).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants