Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected 'S' in Postman on consuming REST Api

I wrote very basic REST service in SpringBootApplication. When I tried to hit the api I am getting response like Unexpected 'S'

This is my api,

@RestController
    @RequestMapping(value="/rally")
    public class CreateProjectApi {

        @RequestMapping(value = "/createProject", 
                produces = { "application/json" }, 
                consumes = { "application/json" }, 
                method = RequestMethod.GET)
        public ResponseEntity<String> createProj() {
            String s = "Success...";
            return new ResponseEntity<String>(s, HttpStatus.OK);
        }

    }

This is my POM.XML,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.altimetrik.rally</groupId>
    <artifactId>RallyRestApi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>RallyRestApi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <!-- Setup Spring MVC & REST, use Embedded Tomcat -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.rallydev.rest</groupId>
            <artifactId>rally-rest-api</artifactId>
            <version>2.2.1</version>
        </dependency>
</dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I am hitting the api through postman with content-type:application/json in header. But I am getting Unexpected 'S' as response. I have followed this link unexpected 's' in response of restful service but I didnt get any solution from that. Can anyone help me out in this?

like image 878
Nithyananth Avatar asked Jun 13 '17 02:06

Nithyananth


People also ask

Why I am getting response in html in Postman?

This means that the api which you are hitting have some issues. So in your case most probably you have some issues with your php laravel code, that's why whenever you are calling that api then html code was returned on behalf of that.

How do you see the actual request in Postman?

You can right click on the main Postman window > Inspect element. In the Network tab, you'll be able to see the request when you click the Send button. Clicking on the request in the Network tab will show you the response payload.

Does Postman support JSON?

Test and access JSON properties To make it convenient and quick, you can fork the following Postman collection or click on the Run in Postman button below. Code snippet to get all the properties of objects within an array: pm.


1 Answers

As explained in the comments by both user7790438 and nbrooks, the issue is that your response will be plain text (Success...), even though you tell it to load JSON (by providing the Content-Type: application/json header).

To return JSON, you should return some kind of object, for example, you could create a ProjectStatus class like this:

public class ProjectStatus {
    private String status;

    public ProjectStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }
}

Now, in your REST controller you should return a ResponseEntity<ProjectStatus>, like this:

return new ResponseEntity<ProjectStatus>(new ProjectStatus("Success..."), HttpStatus.OK);

This will result in the following JSON when you use Postman:

{"status": "Success..."}

You can pretty much return any object you'd like. An example would be to return the created project in your response.


Alternatively, if you don't need JSON, you could provide the Content-Type: text/plain header.

like image 188
g00glen00b Avatar answered Sep 16 '22 16:09

g00glen00b